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

标签 c++ vector 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

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/

相关文章:

c++ - 错误 : ‘hour’ was not declared in this scope

c++ - 从文件中读取数据对

haskell - 为什么 GHCi 在此实例声明中报告 "A constraint must be a monotype"?

r - 如何测试向量是否包含重复元素?

c# - 设置double中点后的位数

java - 将字符串与android中的字符串提取进行比较

c++ - 如何使用 vector 字符串标记构建表达式树?

c++ - MFC:处理上下文菜单消息的最佳位置?

c++ - eclipse cdt - 如何在 C/C++ 编辑器窗口中设置工具提示窗口的背景颜色?

c - 如何在 AVX 或 SSE 指令中进行间接加载(聚集-分散)?