std::string
我想初始化从构造函数获得的大小指针数组。另外,我想对两个int
数组执行相同的操作,但下面的代码无法编译:
class MyQuickInitArray
{
public:
MyQuickInitArray(int size)
{
if(size <= 0)
{
throw new std::exception;
}
_size = size;
_counter = 0;
A = new std::string[size];
B = new int[size];
C = new int[size];
}
std::string& operator[](int j) {
if(j > _size)
{
throw std::out_of_range("out of range");
}
if(((B[j]<0) || (B[j]>=_counter)) || (C[B[j]]!=j))
{
// A[j] points to junk
_counter++;
A[j] = new std::string;
B[j] = _counter-1;
C[_counter-1] = j;
return A[j];
}
// the cell was instantiated before
return A[j];
}
~MyQuickInitArray(){};
private:
std::string* A[];
int B[];
int C[];
int _size;
int _counter;
};
如何正确声明从 ctor 获得的大小数组?
编辑:
发生的错误是:
incompatible types in assignment of ‘std::string* {aka std::basic_string<char>*}’ to ‘std::string* [0]
对于int
数组:
incompatible types in assignment of ‘int*’ to ‘int [0]’