将vector声明为类数据成员时出错

min*_*box 2 c++ types 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
Run Code Online (Sandbox Code Playgroud)

PS:我必须在每一行添加4个空格才能复制和粘贴上面的代码,因为它们都没有格式化.有没有更简单的方法?

jua*_*nza 7

这被解析为函数声明:

vector<bool> set(setSize); // function `set`, argument type `setSize`
Run Code Online (Sandbox Code Playgroud)

您需要一个不同的初始化语法:

vector<bool> set = vector<bool>(setSize);
Run Code Online (Sandbox Code Playgroud)

还要注意,给出诸如setwhile之类的名字using namespace std;是一个非常糟糕的主意.using namespace std;无论如何,在大多数情况下是一个坏主意.