简体   繁体   English

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

[英]Error when declaring vector as a class data member

I am still wondering why an error message keeps on appearing when trying to declare a vector: 我仍然想知道为什么在尝试声明向量时会出现错误消息:

"Unknown type name 'setSize'" “未知的类型名称'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 : I had to add 4 spaces to every line in order to copy and paste the above code, because it all went out of format. PS:我必须在每一行添加4个空格才能复制并粘贴上面的代码,因为它们都没有格式化。 Is there an easier way? 有更容易的方法吗?

This is parsed as a function declaration: 这被解析为函数声明:

vector<bool> set(setSize); // function `set`, argument type `setSize`

You need a different initialization syntax: 您需要一个不同的初始化语法:

vector<bool> set = vector<bool>(setSize);

Also note that giving things names such as set while using namespace std; 另请注意,在using namespace std;时给出诸如set using namespace std; is a very bad idea. 是一个非常糟糕的主意。 using namespace std; is a bad idea in most cases anyway . 无论如何,在大多数情况下是一个坏主意

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM