error: call of overloaded ' .... ' is ambiguous

T

thell123

I want to call a function by call-by-reference, it's set/get functions
in another module/file, and I don't want public variables, but get an
error when I want to read the value with & :

error: call of overloaded 'Private_var(int&)' is ambiguous
note: candidates are void Private_var(int)
note: candidates are void Private_var(int&)


How should I call the get function ?

My code :


int my_private_var;

void Private_var(int var);
void Private_var(int& var);


void main()
int a=5;
int b=0;
Private_var(a);
// getting the private variable
Private_var((int&)b);
}

void Private_var(int var) {
my_private_var = var;
}

void Private_var(int& var) {
var = my_private_var;
}
 
B

Bart van Ingen Schenau

I want to call a function by call-by-reference, it's set/get functions
in another module/file, and I don't want public variables, but get an
error when I want to read the value with & :

error: call of overloaded 'Private_var(int&)' is ambiguous
    note: candidates are void Private_var(int)
    note: candidates are void Private_var(int&)

How should I call the get function ?

The ususl idiom is to let the setter have a (value or const reference)
argument and let the getter return the value without taking any
arguments:

void Private_var(int var); // Setter
int Private_var(); // Getter

This way, the compiler can't get confused which function you mean, and
more importantly, other people (including you a week from now) will
also not get confused which function is meant.

Bart v Ingen Schenau
 
T

thell123

I can see that, but the reason initally, why I used the notation, was
that I want to return an indication, whether the variable is
initialized..

bool Private_var(int var);
bool Private_var(int &var);
 
S

SG

I can see that, but the reason initally, why I used the notation, was
that I want to return an indication, whether the variable is
initialized..

bool Private_var(int var);
bool Private_var(int &var);

Overloading is quite useful -- especially in generic programming. But
here you want to do two completely different things. One is supposed
to change the global state and the other is to return a copy of the
state. Usually that's not a situation you would use overloading for.

Also "int" can't be overladed with "int&". The trick with references
is that they can bind to "lvalues". So,

void foo(int& t) {
t = 23;
}

int main() {
int b = 42;
foo(b);
return b; // returns 23
}

even without your cast. That's actually the intended behaviour.
Because of this the compiler doesn't know what function you meant --
both of your functions are valid candidates.

Cheers!
SG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top