我正在构建一个游戏,我正在编译,看看会出现什么样的错误,并且有一个非常普遍且非常令我费解:
1>c:\users\owner\desktop\bosconian\code\bosconian\ship.h(9) : error C2460: 'Ship::Coordinate' : uses 'Ship', which is being defined
这也适用于SpaceObject类及其所有其他派生。 Coordinate类只是一个二维矢量类,如果重要的话,只引用一个环境类。
Environment类引用了SpaceObject,但我不相信这是一个问题(如果我错了就纠正我)
这是我在C ++中的第一个大型项目,我想知道这是否是一个常见的新手错误,有一个更明显的解决方案。如果重要的话,SpaceObject类层次结构并不是一直填充的,而是填充在SpaceObject和Ship类之后的几个层次。
。
。
。
编辑:这是对评论的回应。
- 当我说引用时,我指的是在另一个类中引用该类。就像在SpaceObject类中一样,我引用了Coordinate类:
协调*位置
环境确实引用了SpaceObject指针,但我看不到我不能引用那样的类......我的意思是,我的所有类都必须以某种方式相互关联,对吧?
为了回应粘贴代码,它有点难,因为它们是巨大的类,但这里是错误指向的行:
public:
Ship(Coordinate * positionObject_, int direction_, int possibleDirections_, int maxHealth_, Component * objectSectors_, int numOfObjectSectors_, double speed_);//this is the line
void move();//handles the actual translation of calculated move on the map (i.e. bounds checking)
答案 0 :(得分:12)
你有这样的事情:
class Ship
{
class Coordinate
{
Ship m_ship;
};
Coordinate m_coordinate;
};
问题在于每个Ship对象都包含一个Coordinate成员,该Coordinate包含Ship,ad nauseum。如果允许继续,那么船的大小将变得无限大。你真正想要的是用指针或引用替换那些成员变量 - 指针/引用具有已知的大小(例如32位CPU上的4个字节),并且可以在不知道有关被指向/引用的类型的任何信息的情况下声明到。