mahmood
mahmood

Reputation: 24715

invalid initialization of reference of type

There is a function isInstruction() in my code (not mine) which is used for setting and getting a member with no problem. Now I added my own function for similar purpose state(). Like this:

struct foo {
  bool & isInstruction() {
    return isInst;    // no problem
  }
  int & state() {
    return state;   //ERROR
  } 
private:
  bool isInst;
  int state;  
};

I have no problem with the first function. But for the second one, I get

error: invalid initialization of reference of type ‘int&’ from expression of type 
‘<unresolved overloaded function type>’

Then question is what is the difference between these two functions. Am I missing something?

Upvotes: 0

Views: 2104

Answers (1)

NPE
NPE

Reputation: 500367

The difference is that the two entities (the member variable and the member function) are sharing the same name, state, which is what's causing the problem.

Try renaming one of them.

Upvotes: 5

Related Questions