Visual Studio 2013 - C++,强制转换构造函数,"没有运算符"break"匹配这些操作数

visual studio 2013 - C++, cast constructor, "no operator "=" matches these operands

本文关键字:运算符 操作数 break 构造函数 2013 Studio C++ 转换 Visual      更新时间:2023-10-16

你好,我的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 gameTable : public gameGraphics
{
private:
    base** table;
public:
    gameTable();
    ~gameTable();
}

当我构造gameTable类时,我构造了64个基本对象,其RANDOMR、G、B值从0到255。

因此,当程序继续进行时,表中的一些元素会"进化"并成为superBase的元素。所以这是我的问题,我不知道该怎么做。我试过了,

这似乎不能正常工作。

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

另一个版本:

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

我的问题是如何将表中的一个元素演化为superBase类元素。正如我所知,它可以使用与基类元素相同的指针。

问候和感谢您的帮助!

"no operator"="匹配这些操作数

此处:

table[column][row] = new superBase;

table[a][b]base的左值引用。您正在将调用new的结果传递给它。这将返回指向superBase的指针。那项任务不起作用。这个将编译

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

但是您会得到对象切片。您需要找到一种方法来存储指向基类型的(智能)指针。

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

相关文章: