Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用未声明的标识符C++; 我最近一直在学习C++,我一直在尝试创建一个简单的类,分割成一个头文件和一个源文件。然而,我似乎一直在犯这样的错误: ship.cpp:21:9: error: use of undeclared identifier 'image' return image; ^ 1 error generated._C++_Class_Oop_Allegro5 - Fatal编程技术网

使用未声明的标识符C++; 我最近一直在学习C++,我一直在尝试创建一个简单的类,分割成一个头文件和一个源文件。然而,我似乎一直在犯这样的错误: ship.cpp:21:9: error: use of undeclared identifier 'image' return image; ^ 1 error generated.

使用未声明的标识符C++; 我最近一直在学习C++,我一直在尝试创建一个简单的类,分割成一个头文件和一个源文件。然而,我似乎一直在犯这样的错误: ship.cpp:21:9: error: use of undeclared identifier 'image' return image; ^ 1 error generated.,c++,class,oop,allegro5,C++,Class,Oop,Allegro5,我已将源代码包括在下面: 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_D

我已将源代码包括在下面:

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;
}
#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
#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;
}
#包括
#包括
#包括
#包括
#包括
int main(int argc,字符**argv){
快板显示*DISPLAY=nullptr;
ALLEGRO_位图*图像=nullptr;
如果(!al_init()){
显示本地消息框(显示“错误”、“错误”、“初始化快板失败”、空、快板消息框错误);
返回0;
}
如果(!al_init_image_addon()){
al_show_native_message_box(显示,“错误”,“错误”,“初始化al_init_image_addon失败!”,NULL,ALLEGRO_MESSAGEBOX_Error);
返回0;
}
显示=al_创建_显示(800600);
如果(!显示){
al_show_native_message_box(显示,“错误”,“错误”,“初始化显示失败!”,NULL,ALLEGRO_MESSAGEBOX_Error);
返回0;
}
船舶(“image.jpg”);
al_draw_位图(ship.get_image(),200,200,0);
al_flip_display();
al_rest(2);
返回0;
}
ship.h:

#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;
}
#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
#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;
}
\ifndef SHIP\u H
#定义船舶
#包括
#包括
#包括
班轮
{
快板位图*图像;
私人:
整数宽度;
内部高度;
公众:
Ship(std::字符串图像文件);
快板位图*获取图像();
};
#恩迪夫
ship.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;
}
#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
#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;
}
#包括
#包括
#包括
#包括
#包括
Ship::Ship(std::字符串图像_文件){
image=al_load_位图(image_file.c_str());
if(image==nullptr){

std::cout您定义的函数不正确。
get\u image()
Ship
类的成员。您的定义创建了一个独立函数

ALLEGRO_BITMAP *get_image(){
应该是:

ALLEGRO_BITMAP* Ship::get_image(){

(为了可读性重新定位星号)

您定义的函数不正确。
get_image()
Ship
类的成员。您的定义创建了一个独立的函数

ALLEGRO_BITMAP *get_image(){
应该是:

ALLEGRO_BITMAP* Ship::get_image(){

(为了可读性重新定位星号)

按照当前的定义,
get_image()
只是一个与您的类无关的函数。它位于
ship.cpp
中这一事实无关紧要。由于您试图实现
ship
类的方法,因此需要使用
ship::
前缀定义实现:

ALLEGRO_BITMAP* Ship::get_image() {
    return image;
}

按照当前的定义,
get_image()
只是一个与您的类无关的函数。它位于
ship.cpp
中这一事实无关紧要。由于您试图实现
ship
类的方法,因此需要使用
ship::
前缀定义实现:

ALLEGRO_BITMAP* Ship::get_image() {
    return image;
}

哦,没有发现。谢谢!顺便说一句,将星号与返回类型放在一起是一种惯例,因为这样可以提高可读性吗?例如,
ALLEGRO\u位图*
ALLEGRO\u位图*
相反?这实际上只是一种风格。当它是函数的返回值时,我更喜欢将星号放在左边,但当它是变量时,我更喜欢将星号放在右边可以声明或取消引用。哦,没有发现。谢谢!顺便问一下,将星号与返回类型放在一起是否是惯例,因为这样可以提高可读性?例如,
ALLEGRO\u位图*
ALLEGRO\u位图*
相反?这实际上只是一种风格。当它是函数的返回值时,我更喜欢将星号放在左边但在右边,当它是变量声明或取消引用时。