C++ 类内使用类变量创建对象数组?

新手上路,请多包涵

代码:

class SparseMat;
class Trituple {
    friend class SparseMat;
private:
    int row, col;
    int data;
};

class SparseMat {
public:
    SparseMat(int MAXROW, int MAXCOL, int NonZeroterms) { Rows = MAXROW; Cols = MAXCOL; this->NonZeroterms = NonZeroterms; }
    SparseMat MAT_transpose(SparseMat b);
private:
    int Rows, Cols, NonZeroterms;
    Trituple SMArray[NonZeroterms];
};

2、问题
在SparseMat类内定义三元数组类的对象数组,但是vs2017报错,说我非静态成员引用必须与对象绑定。这该怎么改啊?

阅读 3.9k
1 个回答
class SparseMat {
public:
    SparseMat(int MAXROW, int MAXCOL, int NonZeroterms);
    SparseMat MAT_transpose(SparseMat b);
private:
    int Rows, Cols, NonZeroterms;
    Trituple* SMArray;
};

SparseMat::SparseMat(int MAXROW, int MAXCOL, int NonZeroterms)
    :Rows(MAXROW), Cols(MAXCOL), NonZeroterms(NonZeroterms)
{
     SMArray = new Trituple[NonZeroterms];
}
推荐问题