使用未声明的标识符 C++
Use of undeclared identifier C++
我最近一直在学习 C++,我一直在尝试创建一个简单的 class 拆分为头文件和源文件。但是,我似乎不断收到此错误:
ship.cpp:21:9: error: use of undeclared identifier 'image'
return image;
^
1 error generated.
我在下面包含了源代码:
main.cpp:
#include <iostream>
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_native_dialog.h>
#include <ship.h>
int main(int argc, char **argv){
ALLEGRO_DISPLAY *display = nullptr;
ALLEGRO_BITMAP *image = nullptr;
if(!al_init()){
al_show_native_message_box(display, "Error", "Error", "Failed to initialise allegro", NULL, ALLEGRO_MESSAGEBOX_ERROR);
return 0;
}
if(!al_init_image_addon()) {
al_show_native_message_box(display, "Error", "Error", "Failed to initialize al_init_image_addon!", NULL, ALLEGRO_MESSAGEBOX_ERROR);
return 0;
}
display = al_create_display(800,600);
if(!display) {
al_show_native_message_box(display, "Error", "Error", "Failed to initialize display!", NULL, ALLEGRO_MESSAGEBOX_ERROR);
return 0;
}
Ship ship("image.jpg");
al_draw_bitmap(ship.get_image(), 200, 200, 0);
al_flip_display();
al_rest(2);
return 0;
}
ship.h:
#ifndef SHIP_H
#define SHIP_H
#include <iostream>
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
class Ship
{
ALLEGRO_BITMAP *image;
private:
int width;
int height;
public:
Ship(std::string image_file);
ALLEGRO_BITMAP *get_image();
};
#endif
ship.cpp:
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_native_dialog.h>
#include <iostream>
#include <ship.h>
Ship::Ship(std::string image_file){
image = al_load_bitmap(image_file.c_str());
if(image == nullptr){
std::cout << "Ship went down." << std::endl;
}
std::cout << "Ship loaded successfully." << std::endl;
}
ALLEGRO_BITMAP *get_image(){
return image;
}
您定义的函数有误。 get_image()
是 Ship
class 的成员。您的定义创建了一个独立函数。
ALLEGRO_BITMAP *get_image(){
应该是:
ALLEGRO_BITMAP* Ship::get_image(){
(为了便于阅读而重新定位星号)
按照目前的定义,get_image()
只是一个函数,与您的 class 无关。它位于 ship.cpp
中这一事实无关紧要。由于您正在尝试实现 Ship
class 的 方法 ,因此您需要使用 Ship::
前缀定义实现:
ALLEGRO_BITMAP* Ship::get_image() {
return image;
}
我最近一直在学习 C++,我一直在尝试创建一个简单的 class 拆分为头文件和源文件。但是,我似乎不断收到此错误:
ship.cpp:21:9: error: use of undeclared identifier 'image'
return image;
^
1 error generated.
我在下面包含了源代码:
main.cpp:
#include <iostream>
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_native_dialog.h>
#include <ship.h>
int main(int argc, char **argv){
ALLEGRO_DISPLAY *display = nullptr;
ALLEGRO_BITMAP *image = nullptr;
if(!al_init()){
al_show_native_message_box(display, "Error", "Error", "Failed to initialise allegro", NULL, ALLEGRO_MESSAGEBOX_ERROR);
return 0;
}
if(!al_init_image_addon()) {
al_show_native_message_box(display, "Error", "Error", "Failed to initialize al_init_image_addon!", NULL, ALLEGRO_MESSAGEBOX_ERROR);
return 0;
}
display = al_create_display(800,600);
if(!display) {
al_show_native_message_box(display, "Error", "Error", "Failed to initialize display!", NULL, ALLEGRO_MESSAGEBOX_ERROR);
return 0;
}
Ship ship("image.jpg");
al_draw_bitmap(ship.get_image(), 200, 200, 0);
al_flip_display();
al_rest(2);
return 0;
}
ship.h:
#ifndef SHIP_H
#define SHIP_H
#include <iostream>
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
class Ship
{
ALLEGRO_BITMAP *image;
private:
int width;
int height;
public:
Ship(std::string image_file);
ALLEGRO_BITMAP *get_image();
};
#endif
ship.cpp:
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_native_dialog.h>
#include <iostream>
#include <ship.h>
Ship::Ship(std::string image_file){
image = al_load_bitmap(image_file.c_str());
if(image == nullptr){
std::cout << "Ship went down." << std::endl;
}
std::cout << "Ship loaded successfully." << std::endl;
}
ALLEGRO_BITMAP *get_image(){
return image;
}
您定义的函数有误。 get_image()
是 Ship
class 的成员。您的定义创建了一个独立函数。
ALLEGRO_BITMAP *get_image(){
应该是:
ALLEGRO_BITMAP* Ship::get_image(){
(为了便于阅读而重新定位星号)
按照目前的定义,get_image()
只是一个函数,与您的 class 无关。它位于 ship.cpp
中这一事实无关紧要。由于您正在尝试实现 Ship
class 的 方法 ,因此您需要使用 Ship::
前缀定义实现:
ALLEGRO_BITMAP* Ship::get_image() {
return image;
}