D
David
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);
}
-----------
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);
}
-----------