John Du
John Du

Reputation: 219

error C2582: 'operator =' function is unavailable in

I have a class,

class Points
{
 public:
 Points();
}

and another,

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

 Points myPoints;
}

Now in OtherPoints() constructor I am trying to create a Point variable like,

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

and get the error,

error C2582: 'operator =' function is unavailable in 'Points'

Upvotes: 1

Views: 2215

Answers (2)

Xxcc
Xxcc

Reputation: 91

I don't think myPoints=Points(); is needed;

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

Upvotes: 2

Santosh Sahu
Santosh Sahu

Reputation: 2244

Here is the code I compiled and it's compiling very fine,

 #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;
 }

And the output is,

constructor points called

constructor points called

constructor otherpoints called

constructor points called

destructor points called

destructor otherpoints called

destructor points called

destructor points called

I didn't get any assignment error.

Note: Whenever you do inheritence make base class destructor as virtual.

Upvotes: 0

Related Questions