gpt4 book ai didi

c++ - Visual Studio 2012 Express编译器无法正确将头文件链接在一起

转载 作者:太空宇宙 更新时间:2023-11-04 15:23:41 26 4
gpt4 key购买 nike

我正在为一个小的2D游戏设置一些框架。现在,我只有几个类,但是我立即陷入了编译器问题。

我仅使用main函数运行了该程序,因此可以确认Allegro(图形库)库链接已正常工作。

我已将我的所有头文件(.h)放在解决方案资源管理器的“头文件”部分下,并将我的所有源文件(.cpp)放在“源文件”部分中。根据本Allegro教程,我仅修改了默认设置:http://wiki.allegro.cc/index.php?title=Windows,_Visual_Studio_2010_and_Allegro_5。它不应该以破坏性的方式影响任何事物。

我有几个文件,所以我将列出每个文件。我将跳过一些代码以减小大小和冗余。当我省略任何代码或不做任何逐字记录时,我会在代码中添加注释。 (编辑:我实际上没有跳过任何代码)

main.cpp

#include "common.h"

int main(int argc, char **argv)
{
ALLEGRO_DISPLAY *display = NULL;
World* world = new World(640, 480);

if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}

display = al_create_display(640, 480);
if(!display) {
fprintf(stderr, "failed to create display!\n");
return -1;
}

al_clear_to_color(al_map_rgb(0,0,0));

world->draw(display);

al_flip_display();

al_rest(10.0);

al_destroy_display(display);

return 0;
}


普通

#if !defined(COMMON_INC)
#define COMMON_INC

#include "World.h"
#include "Pane.h"
#include <stdio.h>
#include <allegro5/allegro.h>

#endif


世界

#if !defined(WORLD_INC)
#define WORLD_INC

#include "common.h"
#include "Pane.h"

class World{
public:
World(int wp, int hp);
void draw(ALLEGRO_DISPLAY* display);
void update();


protected:

private:
Pane* panel;
int heightPix, widthPix;

};

#endif


World.cpp

#include "common.h"

World::World(int wp, int hp){
widthPix = wp;
heightPix = hp;
panel = new Pane(this, 10, 10, 300, 400);
return;
}

void World::draw(ALLEGRO_DISPLAY* display){
panel->draw(display);
return;
}


窗格

#if !defined(PANE_INC)
#define PANE_INC

#include "common.h"

class Pane{
public:
Pane(World* w, int xv, int yv, int wi, int he);
World* getWorld();
int getZ();
void draw(ALLEGRO_DISPLAY* display);
ALLEGRO_BITMAP* getBackground();

protected:
void setXY(int xv, int yv);
void setWidthHeight(int wi, int he);
void setZ(int zv);
void setBackground(ALLEGRO_BITMAP* ba);

private:
int z;
int x, y; //pixels
int width, height; //pixels
World* world;
ALLEGRO_BITMAP* background;
};

#endif


Pane.cpp

#include "common.h"

Pane::Pane(World* w, int xv, int yv, int wi, int he){
this->setWidthHeight(wi, he);
this->setXY(xv, yv);
this->world = w;
}

World* Pane::getWorld(){
return world;
}

void Pane::setXY(int xv, int yv){
x = xv;
y = yv;
return;
}

void Pane::setWidthHeight(int wi, int he){\
width = wi;
height = he;
return;
}

void Pane::setZ(int zv){
z = zv;
return;
}

void Pane::draw(ALLEGRO_DISPLAY* display){
if(background != NULL)
al_draw_bitmap(background, x, y, 0);
else{
background = al_create_bitmap(width, height);

al_set_target_bitmap(background);

al_clear_to_color(al_map_rgb(255, 0, 255));

al_set_target_bitmap(al_get_backbuffer(display));

al_draw_bitmap(background, x, y, 0);
}
return;
}


编译器在构建时会产生以下错误报告:(我称游戏为madscientist,因为它应该以炼金术为主题)

1>------ Build started: Project: MadScientist, Configuration: Debug Win32 ------
1> main.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(8): error C2061: syntax error : identifier 'World'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): warning C4183: 'getWorld': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(11): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): warning C4183: 'getBackground': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(18): error C2061: syntax error : identifier 'ALLEGRO_BITMAP'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(10): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\main.cpp(22): error C2660: 'World::draw' : function does not take 1 arguments
1> World.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(8): error C2061: syntax error : identifier 'World'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): warning C4183: 'getWorld': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(11): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): warning C4183: 'getBackground': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(18): error C2061: syntax error : identifier 'ALLEGRO_BITMAP'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(10): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(6): error C2661: 'Pane::Pane' : no overloaded function takes 5 arguments
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(10): error C2511: 'void World::draw(ALLEGRO_DISPLAY *)' : overloaded member function not found in 'World'
1> c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(7) : see declaration of 'World'
1> Pane.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(8): error C2061: syntax error : identifier 'World'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): warning C4183: 'getWorld': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(11): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(12): warning C4183: 'getBackground': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(18): error C2061: syntax error : identifier 'ALLEGRO_BITMAP'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(24): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(10): error C2061: syntax error : identifier 'ALLEGRO_DISPLAY'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.cpp(3): error C2511: 'Pane::Pane(World *,int,int,int,int)' : overloaded member function not found in 'Pane'
1> c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(6) : see declaration of 'Pane'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.cpp(9): error C2556: 'World *Pane::getWorld(void)' : overloaded function differs only by return type from 'int *Pane::getWorld(void)'
1> c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9) : see declaration of 'Pane::getWorld'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.cpp(9): error C2371: 'Pane::getWorld' : redefinition; different basic types
1> c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9) : see declaration of 'Pane::getWorld'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.cpp(10): error C2065: 'world' : undeclared identifier
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.cpp(30): error C2511: 'void Pane::draw(ALLEGRO_DISPLAY *)' : overloaded member function not found in 'Pane'
1> c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(6) : see declaration of 'Pane'
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


您拥有我所做的所有信息。我将为您总结一下,以免您浏览所有内容。


将类分为头接口和源实现
common.h被包含在所有文件中。它本身包括其他头文件中的所有类定义
编译时,编译器无法将其他头文件中定义的类识别为数据类型。如您所料,错误会逐渐减少。
实时错误检查器不会注册任何错误。例如,将鼠标悬停在单词“ World”上用作 Pane中的数据类型时,它将完美地识别该类型。实时错误检查器是在编译器之前红色强调错误的功能。
除了前面提到的修改,此Visual Studio 2012 Express是新的。该项目被创建为一个空的C ++项目。在添加许多标题和源之前, main函数已正确编译。


感谢您的任何答复。如果您有任何问题,请询问。我在Visual Studio中遇到的问题总是让我感到困扰。

编辑:

我的循环头逻辑由头保护器保护。我认为这种冗余不是问题。

我尝试从头文件中删除所有包含,但Allegro库中的包含除外。这似乎工作得更好,但是仍然存在一些奇怪的问题。为什么包含行为导致这些数据类型出错仍然是一个谜。这是新的错误日志:

1>------ Build started: Project: MadScientist, Configuration: Debug Win32 ------
1> main.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> World.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(6): error C2065: 'panel' : undeclared identifier
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(11): error C2065: 'panel' : undeclared identifier
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(11): error C2227: left of '->draw' must point to class/struct/union/generic type
1> type is ''unknown-type''
1> Pane.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


第2轮编辑:

我切换了代码,以便common.h仅包括stdio和Allegro。所有头文件和源文件都包括common.h,然后分别包括它们正在使用的任何类的头文件。 Pane.cpp包括Pane.h和common.h。 World.h包括Pane.h和common.h。

错误日志显示为:

1>------ Build started: Project: MadScientist, Configuration: Debug Win32 ------
1> main.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C2061: syntax error : identifier 'World'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): warning C4183: 'getWorld': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> World.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(9): error C2061: syntax error : identifier 'World'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(10): warning C4183: 'getWorld': missing return type; assumed to be a member function returning 'int'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\pane.h(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.cpp(7): error C2661: 'Pane::Pane' : no overloaded function takes 5 arguments
1> Pane.cpp
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C2143: syntax error : missing ';' before '*'
1>c:\users\ethoma\documents\visual studio 2012\projects\madscientist\madscientist\world.h(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


这就是我现在看到我的包含订单的方式:


主要包括共同
通用定义COMMON_INC
常见的包括stdio和allegro
主要包括World.h
世界定义WORLD_INC
世界包括普通,被封锁。但这应该可以,因为已经包含了common
世界包括窗格
窗格定义了PANE_INC
窗格包括common,已被阻止。但这应该可以,因为已经包含了common。
窗格包括“世界”。由于主要对象已包含World,因此已被阻止。因为世界已经被包括在内,这不应该吗?如果我需要多次多次包含文件(对于每个使用该文件的文件一次),为什么还要拥有编译器保护程序?


第三轮编辑
我从这里的答案和评论中学到了很多东西。事实证明,根据Wikipedia所说,“循环依赖通常是由缺乏经验的程序员引入的,他们需要实现某种回调功能。” Pane使用World和World使用Pane的事实是设计缺陷。在Java中,这对我来说都很好。这很容易。我认为这是通知“拥有”对象行为的实体的最佳方法。

事实证明,这是一个糟糕的设计方案。对象不应该依赖于它们的“拥有者”。相反,我需要实现一个观察者系统,在该系统中,窗格可以告诉世界它已经更新了状态。

阅读Wikipedia可以清除我的所有问题,因此我现在认为该问题已经结束。感谢贡献者为学习型程序员献计献策。

最佳答案

您的头文件可能无法正确链接。您以循环方式包含了标题。您的头文件包含common.h,而common.h又包含其他头,例如World.hPane.h

这是任何编译器都不能编译的。您必须开发从低级标题到高级标题的标题层次结构,并确保较高级别的标题仅包含较低级别的标题。这样,您将确保没有圆形夹杂物。

无论如何,为什么您的common.h包含World.hPane.h?这看起来是一个明显的错误。 common.h是低级标题。让World.hPane.h包含它,但不要在World.h中包含Pane.hcommon.h

请注意,在这种情况下,标头防护不能解决任何问题。他们只是简单地确保包含周期不会无限。它们破坏了循环,但是如果存在依赖关系,它们将无法帮助您解决声明之间的循环依赖关系。

查看处理main.cpp时发生的情况


main.cpp包括common.h
common.h定义COMMON_INC
common.h包括World.h
World.h定义WORLD_INC
World.h包括common.h。整个common.h被include防护跳过,因为COMMON_INC在步骤2中定义。
World.h包括Pane.h
Pane.h定义PANE_INC
Pane.h包括common.h。整个common.h被include防护跳过,因为COMMON_INC在步骤2中定义。
Pane.h是指类型World。类型World是未知的,因为我们尚未在World.h中定义它。错误!

关于c++ - Visual Studio 2012 Express编译器无法正确将头文件链接在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13774026/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com