2014-04-22 132 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。您需要找到一种方法来存储(智能)指向基本类型的指针。

除此之外,你的基类需要一个虚拟析构函数。而且你不应该直接调用析构函数。

相关问题