2014-04-22 61 views
1

你好我有一個小問題,我的C++項目C++,投構造,「沒有運營商‘=’匹配這些操作數

首先,我得到了類:

class base 
{ 
protected: 
    int R, G, B; 
public: 
    base(); 
    ~base(); 
}; 

和第二類:

class superBase : 
    public base 
{ 
public: 
    superBase(){R=0; G=0; B=0}; 
    ~superBase(); 
}; 

和含有鹼class'es的矩陣中的最後類:

class gameTable : public gameGraphics 
{ 
private: 
    base** table; 
public: 
    gameTable(); 
    ~gameTable(); 
} 

當我構建gameTable類別i構造64個基礎對象與RANDOM R,G,B值從0到255

因此,當節目的推移,一些在表礦elemntes「演變」和變得超強鹼的。所以這裏是我的問題,我不知道該怎麼做。我試過這個,

這似乎無法正常工作。

 superBase newBase; 
     table[column][row].~base(); 
     table[column][row] = newBase; 

和其他版本:

table[column][row].~base(); 
    table[column][row] = new superBase; 

我的問題是如何對錶格的一個元素演變爲超強類元素。據我所知,它可以使用與基類元素相同的指針。

問候和感謝您的幫助!

+1

「table」的定義在哪裏? – Soren

+0

'new T'返回一個指針。你的「2D數組」不包含指針。另外,不要這樣調用析構函數。 – juanchopanza

+1

'table [column] [row]。〜base();'< - 不要調用這樣的析構函數。如果你用'new'分配,你需要'刪除'。但是你應該儘可能使用智能指針和向量。 – crashmstr

回答

1

「沒有運營商」 =」這些操作數

這裏匹配:

table[column][row] = new superBase; 

table[a][b]base左值參考你把它傳遞給new呼叫的結果。這。返回指向superBase的指針,該賦值不能工作,這個將編譯

table[column][row] = superBase(); 

但你會得到object slicing。您需要找到一種方法來存儲(智能)指向基本類型的指針。

除此之外,你的基類需要一個虛擬析構函數。而且你不應該直接調用析構函數。

相關問題