错误 C2582:"运算符 ="函数在 中不可用

error C2582: 'operator =' function is unavailable in

本文关键字:C2582 运算符 错误 函数      更新时间:2023-10-16

我有一个类,

class Points
{
 public:
 Points();
}

另一种是

class OtherPoints : public Points
{
 public:
 OtherPoints ();
 Points myPoints;
}

现在在OtherPoints()构造函数中,我正在尝试创建一个Point变量,比如

OtherPoints::OtherPoints(){ 
    myPoints=Points();
}

并得到错误,

错误C2582:"点"中的"operator="功能不可用

我认为不需要myPoints=Points();

Points myPoints; // This code has already called the constructor (Points();)

这是我编译的代码,它编译得很好,

 #include<iostream>
 using namespace std;
 class points{
    public: points(){cout<<"constructor points called"<<endl;}
            virtual ~points(){cout<<"destructor points called"<<endl;}
 };
 class otherpoints: public points{
                    points x1;
    public: otherpoints(){cout<<"constructor otherpoints called"<<endl;x1=points();}
            ~otherpoints(){cout<<"destructor otherpoints called"<<endl;}
 };
 int main(int argc, char *argv[])
 {
    otherpoints y1=otherpoints();        
    return 0;
 }

输出是

称为的构造函数点

称为的构造函数点

构造函数称为的其他点

称为的构造函数点

称为的析构函数点

析构函数其他称为的点

称为的析构函数点

称为的析构函数点

我没有收到任何作业错误。

注意:无论何时进行继承,都要将基类析构函数设置为虚拟的