0

Please tell what I am doing wrong in this code below, compilor is giving me following error 1. declaration of operator = as non function 2. expected primary expression before const I am not able to identify my mistake

and so on plz help me

#include <iostream>
using namespace std;


class myClass{

    int a;
    int b;

    public:

        myClass();
        myClass(int x, int y);
        const myClass& operator=(const myClass &);  
        void display();     

};


myClass::myClass(){

    a=0;
    b=0;
}
myClass::myClass(int x, int y){
    this->a=x;
    this->b=y;
}

const myClass& myClass::operator=(const class & rightobj){

    if(this!=&rightobj){

        this->a=rightobj.a;
        this->b=rightobj.b;

    }

    return *this;

}

void myClass::display(){

    cout<<a<<endl;
    cout<<b<<endl;
}

int main(){

myClass class1(2,3);
myClass class2;

class2=class1;

class2.display();



return 0;
}
user3215228
  • 313
  • 1
  • 3
  • 13

1 Answers1

4

Here you have a typo:

const myClass& myClass::operator=(const class & rightobj)
                                       //^^Should be myClass
taocp
  • 23,276
  • 10
  • 49
  • 62