だから私は次の簡単なコードを持っています
#include <iostream>
class Base {
public:
virtual int GetX() const = 0;
virtual int GetY() const = 0;
virtual Base& operator=(const Base&) = 0;
protected:
int x;
};
class Derived : public Base {
public:
Derived(int a = 0, int b = 0):y(b){x=a;};
Base& operator=(const Base&);
int GetX() const{return x;}
int GetY() const{return y;}
void Print(){std::cout << x << y << std::endl;}
private:
int y;
};
Base& Derived::operator=(const Base& t)
{
y = t.GetY();
x = t.GetX();
return *this;
}
int main()
{
Derived A(1,2);
Derived B;
B = A;
A.Print();
}
そして、私の問題は、行 B=A にコメントしない限り、次のエラーが発生するため、演算子 = の定義にあります。
In function `Derived::operator=(Derived const&)':
File.C:(.text._ZN7DerivedaSERKS_[_ZN7DerivedaSERKS_]+0x1f): undefined reference to `Base::operator=(Base const&)'
collect2: error: ld returned 1 exit status
コピー割り当てで多くの回避策を試しましたが、何も機能しません。Base クラスで定義する必要があるのは、別の Derived クラス (built2) が必要であり、理想的には、派生クラス間で同等にすることさえできるからです。
編集:間違ったコードを貼り付けました