我仍然想知道为什么错误消息不断出现 尝试声明 vector 时:
“未知类型名称‘setSize’”
#ifndef INTEGERSET_H
#define INTEGERSET_H
#include <vector>
using namespace std;
class IntegerSet
{
public:
IntegerSet();
IntegerSet(const int [], const int);
IntegerSet & unionOfSets(const IntegerSet &, const IntegerSet &) const;
IntegerSet & intersectionOfSets(const IntegerSet &, const IntegerSet &) const;
void insertElement(int);
void deleteElement(int);
void printSet();
bool isEqualTo(const IntegerSet &);
const int setSize = 10;
vector<bool> set(setSize);
};
#endif
PS :为了复制和粘贴上面的代码,我必须在每一行中添加 4 个空格,因为它们都已超出格式。有没有更简单的方法?
最佳答案
这被解析为函数声明:
vector<bool> set(setSize); // function `set`, argument type `setSize`
你需要一个不同的初始化语法:
vector<bool> set = vector<bool>(setSize);
另请注意,在 using namespace std;
时给事物命名,如 set
是一个非常糟糕的主意。 using namespace std;
is a bad idea in most cases anyway .
关于c++ - 将 vector 声明为类数据成员时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34574723/