【发布时间】:2018-11-27 04:37:39
【问题描述】:
我正在做一个 c++ 程序。这就是我要做的:我创建一个我想要的大小的数组。数组自动填充0。
使用operator += i 必须在i 选择的位置插入1。
示例:
array += 2;
will insert 1 at the index 2 of my array.
但是我该怎么做呢?
我的 .h 文件
#ifndef BITARRAY_H
#define BITARRAY_H
#include <ostream>
class bitArray
{
public:
bitArray(int n);
virtual ~bitArray();
bitArray& operator+=(const bitArray&); //this operator
bitArray& operator-=(const bitArray&);
int& operator[] (int x) {
return sortie[x];
}
protected:
private:
int sortie[];
int n;
};
//ostream& operator<<(ostream&, const bitArray&);
#endif // BITARRAY_H
我在cpp文件中的方法:
bitArray& bitArray::operator+=(const bitArray& i)
{
this ->sortie[i] = 1;
return *this;
}
但它不起作用。我做对了吗?
我的错误是:
no match for 'operator[]' (operand types are 'int [0]' and 'const bitArray')|
提前谢谢你!
【问题讨论】:
-
参数应该是一个int。
-
@rustyx 或
size_t。顺便说一下,您的数组sortie似乎需要动态分配,因为n是一个运行时参数。
标签: c++ arrays class c++11 operator-overloading