鍍金池/ 問答/C++/ C++ 類內(nèi)使用類變量創(chuàng)建對(duì)象數(shù)組?

C++ 類內(nèi)使用類變量創(chuàng)建對(duì)象數(shù)組?

代碼:

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類內(nèi)定義三元數(shù)組類的對(duì)象數(shù)組,但是vs2017報(bào)錯(cuò),說我非靜態(tài)成員引用必須與對(duì)象綁定。這該怎么改?。?/p>

回答
編輯回答
悶油瓶
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];
}
2018年9月6日 10:29