Problem in Const&

hi,

 i'm trying to avoid the copy made during the appel of a function by using const&,

 But it results a compilation error,

 here is a function, it works with <Compteur c> as argument,

 But when i add const& i get the following;(i don't understand it)

 Error; passing 'const Compteur' as 'this' argument of 'unsigned int& Compteur::Val()' discards qualifiers [-fpermissive]|

1
2
3
4
void afficher(Compteur const& c)
{
  cout << "Position : " << c.Val() << endl;
}


Val() is the getter of a private unsigned int called val。

thank u。
Last edited on
Sorry to be annoying, Thank you So much。
Last edited on
In your declaration of Val, make sure you add the const qualifier after the method, like so:

1
2
3
unsigned &Compteur::Val() const {
    ...
}
Last edited on
Smac89 , Thank u very much.
Topic archived. No new replies allowed.