c++ - error : no match for 'operator=' . 尝试从基类继承并从基类初始化?

标签 c++ inheritance polymorphism operator-overloading

我正在尝试声明一个名为 BASE 的基类,但我在使用继承类型 AB 时遇到了问题BASE 类。我遇到了错误

|In member function 'NODE& NODE::operator=(const NODE&)':|
16|warning: no return statement in function returning non-void|
In member function 'void BASE<T, SIZE>::init_A(int) [with T = NODE, unsigned int SIZE = 2u]':|
96|instantiated from here|
39|error: no match for 'operator=' in 'A<NODE, 2u>::DATA[index] = a'|
13|note: candidates are: NODE& NODE::operator=(const NODE&)|


#include <iostream>

class NODE
{
        private:

        public:

        NODE(){}
        ~NODE(){}

};

template <class T, size_t SIZE>
class A;
template <class T, size_t SIZE>

class BASE
{
    protected:

        static T DATA[SIZE];

    public:

        BASE()
        {

        }
        ~BASE(){}
        void init_A(int index)
        {                       
            A<T,SIZE>::DATA[index] = T();            
        }        
};
template <class T, size_t SIZE>
class A : public BASE<T,SIZE>
{
     protected:         

    public:


        A(){}
        ~A(){}

};

template <class T, size_t SIZE>
T BASE<T,SIZE>::DATA[SIZE] = {};
int main()
{
    BASE<NODE,2> base;

    base.init_A(0);    

    return 0;
}

最佳答案

我可以编译它,但它可能无法满足您的要求。 第一个问题是你的赋值运算符 promise 返回一些东西但没有:

NODE& NODE::operator=(const NODE&)
{

}

试试这个

NODE& NODE::operator=(const NODE&)
{
    return *this;
}

第二个问题是

A<T,SIZE> a;
A<T,SIZE>::DATA[index] = a;

DATAT 的数组,不是 A<T,SIZE> . 试试这个

A<T,SIZE>::DATA[index] = T();

最后你需要在某处声明你的静力学。
最后,您需要在某处定义您的静力学。参见 here

关于c++ - error : no match for 'operator=' . 尝试从基类继承并从基类初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17964506/

相关文章:

c++ - Mysql_num_rows() 段错误

c++ - bash 脚本在使用 popen() 时不将返回码返回给调用 C++ 程序

c++ - 我如何将多态属性与 boost::spirit::qi 解析器一起使用?

c++ - 没有 getline 的重载函数实例

php - 捕获与 PHP exec() 分开的输出参数

c++ - 尝试在 C++ 中减少设计模式

c++ - 从模板基类继承构造函数而不重复模板参数?

php - OOP:您将如何在 OOP 中建立艺术家、专辑和歌曲关系

java - java 如何反序列化一个类不在类路径上但其父类(super class)在的对象?

string - Haskell中如何根据字符串解析的结果返回多态类型?