C++ 未声明的标识符

Ost*_*huk 3 c++ undeclared-identifier

我想创建一个海\xe2\x80\x8b\xe2\x80\x8bbattle 游戏。我有两个课程:船舶和细胞。

\n\n
#pragma once\n#include"stdafx.h"\n#include"Globals.h"\n#include<vector>\n#include"MCell.h"\n\n class Ship \n {\n\n private:\n    int lenght;\n    int oriantation;\n    vector<Cell*> cells;\n    vector<Cell*> aroundCells;\n
Run Code Online (Sandbox Code Playgroud)\n\n

...

\n\n
#pragma once\n#include<vector>\n#include"MShip.h"\n\n class Cell\n{\n\nprivate:\n    bool haveShip;\n    bool selected;\n    bool around;\n    int x;\n    int y;\n    Ship* ship; \n
Run Code Online (Sandbox Code Playgroud)\n\n

我有很多这样的错误:

\n\n
1>projects\\seewar\\seewar\\mship.h(13): error C2065: \'Cell\' : undeclared identifier\n1>projects\\seewar\\seewar\\mship.h(13): error C2059: syntax error : \'>\'\n1>projects\\seewar\\seewar\\mship.h(14): error C2065: \'Cell\' : undeclared identifier\n
Run Code Online (Sandbox Code Playgroud)\n\n

代码有什么问题?

\n

Goz*_*Goz 5

那么你的问题在于,当你包含 MCell.h 时,你包含了 MShip.h,它引用了 MCell.h 中定义的 Cell。然而,MShip.h 引用了 MCell.h,由于编译指示一次,它不会被包含在内。如果编译指示曾经不存在,那么您将得到一个无限循环,该循环将使您的编译器堆栈溢出......

相反,您可以使用前向声明。

即从 MShip.h 中删除#include“MCell.h”并将其替换为简单的“class Cell;” 您所有的循环引用问题都会消失:)