By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,486 Members | 1,589 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,486 IT Pros & Developers. It's quick & easy.

why I have compile error

P: n/a
Hi,

why I have compile error as follows?
r.cpp: In function `int main ()':
r.cpp:40: call of overloaded `change(Demo &)' is ambiguous
r.cpp:11: candidates are: void Demo::change (Demo)
r.cpp:23: void Demo::change (Demo &)

-------------program
#include <iostream>

class Demo{
public:
int x, y, z;
public:
Demo(int i, int j, int k){
x =i; y=j; z =k;
}

void change(Demo t){
t.x = 1;
t.y = 2;
t.z = 3;
}

void change(Demo *t){
t->x = 1;
t->y = 2;
t->z = 3;
}

void change(Demo &t){
t.x = 1;
t.y = 2;
t.z = 3;
}

void print(Demo t){
cout << "x=" << t.x <<endl;
cout << "y=" << t.y <<endl;
cout << "z=" << t.z <<endl;
}

};

int main(){
Demo num(10, 20 ,30);
Demo &tt = num;
tt.change(tt);
tt.print(tt);
}
-----------
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 2 Sep 2004 12:34:00 -0700
hu*****@yahoo.com (David) wrote:
why I have compile error as follows?


Because the compiler cannot conclude from your call of the function in
the code whether it should use the reference version or the other one.

The only solution I can think of is renaming this feature/function if
you really need it.

best regards
Moritz Beller
--
web http://www.4momo.de
mail momo dot beller at t-online dot de
gpgkey http://gpg.notlong.com
Jul 22 '05 #2

P: n/a
> void change(Demo t){
t.x = 1;
t.y = 2;
t.z = 3;
}


Are you sure you even need/want this?

It is not going to change the Demo object passed to it...it is going to change
a copy of the Demo object passed to it.
Jul 22 '05 #3

P: n/a
David wrote:
Hi,

why I have compile error as follows?
r.cpp: In function `int main ()':
r.cpp:40: call of overloaded `change(Demo &)' is ambiguous
r.cpp:11: candidates are: void Demo::change (Demo)
r.cpp:23: void Demo::change (Demo &)
The & on line 23 does not tell Demo what type of variable it will
receive as a parameter, but that it should use a reference to that
parameter instead of a copy of the parameter inside Demo::change.

In other words, lines 11 and 23 specify identical parameters (variable
or reference to a variable), and are therefor ambiguous.

-------------program
#include <iostream>

class Demo{
public:
int x, y, z;
public:
Demo(int i, int j, int k){
x =i; y=j; z =k;
}

void change(Demo t){
t.x = 1;
t.y = 2;
t.z = 3;
}

void change(Demo *t){
t->x = 1;
t->y = 2;
t->z = 3;
}

void change(Demo &t){
t.x = 1;
t.y = 2;
t.z = 3;
}

void print(Demo t){
cout << "x=" << t.x <<endl;
cout << "y=" << t.y <<endl;
cout << "z=" << t.z <<endl;
}

};

int main(){
Demo num(10, 20 ,30);
Demo &tt = num;
tt.change(tt);
tt.print(tt);
}
-----------

--
Will Twentyman
email: wtwentyman at copper dot net
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.