mardi 5 avril 2016

Operator Override Error

I am working on the operator overload and I get this error :

> member function 'getAge' not viable: 'this' argument   has type 'const
> Etudiant', but function is not marked const

But i don't understand because i have overide my method out of my class. I have 3 files :

My header file :

class Etudiant
{

string nom;
string prenom;
int age;

    public:
    Etudiant(string,string,int);
    string getNom();
    string getPrenom();
    int getAge();
};
    bool operator ==(Etudiant const& a,Etudiant const& b);

The Etudiant.cpp (I implement the header method )

Etudiant::Etudiant(string nom,string prenom,int age){
    this->nom = nom;
    this->prenom = prenom;
    this->age = age;
}


string Etudiant::getNom(){
    return this->nom;
}

string Etudiant::getPrenom(){
    return this->prenom;
}

int Etudiant::getAge(){
    return age;
}


bool operator == (Etudiant const& a,Etudiant const& b)
{
    return (a.getAge()==b.getAge());
}

And the main.cpp where i am testing my application

int main(){

Etudiant etudiant("gabriel","zaafrani",25);
Etudiant etudiant2("ilan","haddad",28);
if(etudiant==etudiant2){
    cout << "egale"<<endl;
}else{
    cout << "different"<<endl;
}
}

Thank you

Aucun commentaire:

Enregistrer un commentaire