C++ 错误 C2061:语法错误:标识符,但已包含头文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14444301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error C2061: syntax error : identifier, but header file is already included
提问by ryf9059
I have a big game engine to compile and it worked fine until I did some crazy adjustments. The compiler somehow cannot reconize my mean Game object and produce error C2061: syntax error : identifier 'Game'
, and it seems no matter what file I compile the error always points to a file called GameDataLoader.h
even there is absolutely no connection with this two files. Usually I think it is an inclusion problem but the fact is I have Game.h
included in every .h file necessary, right after the inclusion of stdafx.h and I have #pragama once
on top of it. I thought about circular inclusion problem but the #pragma once
should take care of that. Plus this inclusion directives works fine until I changed some file name and other messy stuff. I cleaned the entire solution and tried to rebuild it but still no luck (using VS2012).
I don't know if showing the code will help, but here is the code for gameDataLoader.h
. This isn't a file I wrote but it should work properly.
我有一个大型游戏引擎要编译,它运行良好,直到我做了一些疯狂的调整。编译器以某种方式无法识别我的平均 Game 对象并产生error C2061: syntax error : identifier 'Game'
,而且似乎无论我编译什么文件,错误总是指向一个名为的文件,GameDataLoader.h
即使这两个文件绝对没有联系。通常我认为这是一个包含问题,但事实是我已经Game.h
包含在每个必要的 .h 文件中,就在包含 stdafx.h 之后,我#pragama once
在它之上。我想过循环包含问题,但是#pragma once
应该照顾那个。另外,这个包含指令可以正常工作,直到我更改了一些文件名和其他乱七八糟的东西。我清理了整个解决方案并尝试重建它,但仍然没有运气(使用 VS2012)。我不知道显示代码是否会有所帮助,但这里是gameDataLoader.h
. 这不是我写的文件,但它应该可以正常工作。
#pragma once
#include "stdafx.h"
#include "Game\Game.h" // Game.h included
class GameDataLoader
{
public:
GameDataLoader(){}
~GameDataLoader(){}
void GameDataLoader::loadGameProperties( Game *game, map<wstring, wstring> *properties, wstring gameInitFile);
virtual void loadGame(Game *game, wstring gameInitFile) = 0;
virtual void loadGUI(Game *game, wstring guiInitFile) = 0;
virtual void loadWorld(Game *game, wstring levelInitFile) = 0;
};
And I had errors exactly on those 4 lines with Game object. What could possibly be the problem? Double inclusion? Circular inclusion? Or did not include something and somewhere else?
我在 Game 对象的那 4 行上有错误。可能是什么问题?双重包容?循环包含?或者不包括某些东西和其他地方?
回答by Mike Seymour
Most likely, Game.h
is including GameDataLoader.h
, either directly or indirectly, creating a circular dependency; but you forgot to show us Game.h
, so I can't be sure of that.
最有可能的Game.h
是GameDataLoader.h
,直接或间接地包含 ,创建循环依赖;但是你忘了给我们看Game.h
,所以我不能确定。
If that is the case, then #pragma once
(or the equivalent include guard) will mean that one of the headers will be included before the other, and declarations from the second won't be available to the first. It won't magically include both of them before each other, since that is impossible.
如果是这种情况,那么#pragma once
(或等效的包含保护)将意味着其中一个标头将包含在另一个标头之前,并且第二个标头中的声明将无法用于第一个标头。它不会神奇地将它们都包含在彼此之前,因为这是不可能的。
Luckily, GameDataLoader.h
doesn't need the full definition of Game
, since it only deals with pointers to it. So you can remove the inclusion of Game.h
and just declare class Game;
instead.
幸运的是,GameDataLoader.h
不需要 的完整定义Game
,因为它只处理指向它的指针。因此,您可以删除包含的内容Game.h
并class Game;
改为声明。