我有一个要用作类型的类
class ship{
public:
vector <int> xp;
vector <int> yp;
vector <bool> pos;
shipType vessel;
bool active;
ship(){}
void randommethods(){
blah;
}
};
我想在舰队类中包含一个类型为 ship 的数组。
class fleet{
public:
ship boat[7];
boat(){}
};
我在 fleet 的源文件中包含了 ship.h 的头文件,但它无法将 ship 识别为一种类型。
fleet.cpp:12: error: ISO C++ forbids declaration of ‘boat’ with no type
fleet.cpp:13: error: declaration of ‘int fleet::boat()’
fleet.cpp:11: error: conflicts with previous declaration ‘ship fleet::boat [7]’
我可以使用类似的声明来处理 ship 类型的变量和 ship.cpp 中的所有成员方法。我做错了什么任何帮助将不胜感激。
最佳答案
在您的舰队类代码中,您声明了一个名为 boat 的 array of ship
( shop boat[7] ) 和一个返回 int 的 function 成员
( boat(){} ) 名为船。您尝试在一个 namespace 中声明两个具有相同名称的不同实体,这是错误的。那是编译器试图告诉你的。
我猜你想像这样为 fleet 类 smth 编写一个默认构造函数:
class fleet{
public:
ship boat[7];
fleet(){} //fleet, not boat
};
关于c++ - 类和类型的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5762506/