C++未声明的标识符
我想制作一款海战游戏。我有两个课程:船舶和细胞。
#pragma once
#include"stdafx.h"
#include"Globals.h"
#include<vector>
#include"MCell.h"
class Ship
{
private:
int lenght;
int oriantation;
vector<Cell*> cells;
vector<Cell*> aroundCells;
...
#pragma once
#include<vector>
#include"MShip.h"
class Cell
{
private:
bool haveShip;
bool selected;
bool around;
int x;
int y;
Ship* ship;
我有很多这样的错误:
1>projects\seewar\seewar\mship.h(13): error C2065: 'Cell' : undeclared identifier
1>projects\seewar\seewar\mship.h(13): error C2059: syntax error : '>'
1>projects\seewar\seewar\mship.h(14): error C2065: 'Cell' : undeclared identifier
代码有什么问题?
I want to create a sea battle game. I have two classes: Ship and Cell.
#pragma once
#include"stdafx.h"
#include"Globals.h"
#include<vector>
#include"MCell.h"
class Ship
{
private:
int lenght;
int oriantation;
vector<Cell*> cells;
vector<Cell*> aroundCells;
...
#pragma once
#include<vector>
#include"MShip.h"
class Cell
{
private:
bool haveShip;
bool selected;
bool around;
int x;
int y;
Ship* ship;
And i have got many error like those:
1>projects\seewar\seewar\mship.h(13): error C2065: 'Cell' : undeclared identifier
1>projects\seewar\seewar\mship.h(13): error C2059: syntax error : '>'
1>projects\seewar\seewar\mship.h(14): error C2065: 'Cell' : undeclared identifier
What's wrong with the code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
那么你的问题在于,当你包含 MCell.h 时,你包含了 MShip.h,它引用了 MCell.h 中定义的 Cell。然而,MShip.h 引用了 MCell.h,由于编译指示一次,它不会被包含在内。如果编译指示曾经不存在,那么您将得到一个无限循环,该循环将使您的编译器堆栈溢出......
相反,您可以使用前向声明。
即从 MShip.h 中删除#include“MCell.h”并将其替换为简单的“class Cell;”您所有的循环引用问题都会消失:)
Well your problem lies that when you include MCell.h you include MShip.h which references Cell defined in MCell.h. However MShip.h refers to MCell.h which won't get included because of the pragma once. If the pragma once wasn't there then you'd get an inifinite loop that would stack overflow your compiler ...
Instead you could use a forward declaration.
ie remove the #include "MCell.h" from MShip.h and replace it with, simply, "class Cell;" All your circular references issues will this go away :)
您的头文件相互依赖。然而,必须先阅读其中一篇。因此,您需要重写其中之一(或两者)以不依赖于另一个。您可以通过前向声明类而不是包含定义它们的头文件来做到这一点。
因此,在 MShip.h 中,您应该放置一个声明
class Cell;
而不是包含 MCell.h 和/或反之亦然。Your header files depend on each other. However one of them will have to be read before the other. So you need to rewrite one of them (or both of them) to not depend on the other. You can do that by forward-declaring the classes instead of including the header file that defines them.
So in MShip.h you should put a declaration
class Cell;
instead of including MCell.h and/or vice versa.您需要转发声明类。
Ship.h
Cell.h
并删除圆形包含物。您不需要在另一个标头中包含一个标头,因为您使用的不是完整类型,而是指针。当您使用该类型的具体对象时,需要完整的类定义。对于指针,则不需要。
You need to forward declare the classes.
Ship.h
Cell.h
and remove the circular inclusion. You don't need to include one header in another since you're not working with complete types, but pointers. Full class definition is required when you use concrete objects of the type. In the case of pointers, you don't.