473,320 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

why I have compile error

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
3 1164
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Danny Boelens | last post by:
Hi all, today I ran into a compile error after a compiler upgrade. I made a small example to demonstrate my compile error: template<typename T1, typename T2> class A {}; class B
3
by: Yang Zhang | last post by:
Here is a program: ///////////////////////////////////////////////// #include <iostream> using namespace std ; class A { int a ; A(const A& aA) { a=aA.a ; cout<<"copy constructor...
12
by: JS | last post by:
I use winXP and have installed Cygwin. I use Dev-C++ and the Cygwin compiler, but for some reason I can't compile this code: #include <setjmp.h> #include <stdio.h> #include <stdlib.h> ...
13
by: Adam Blair | last post by:
Is it possible to bind a switch statement to an Enum such that a compile-time error is raised if not all values within the Enum are handled in the switch statement? I realise you can use default:...
2
by: Gustavo | last post by:
After updating Windows 2000 I began to get a weird compile error message: Deleting intermediate files and output files for project 'pp - Win32 Debug'. --------------------Configuration: pp -...
3
by: Peter | last post by:
Hi, I am trying to compile an existing project (originally c) in .NET (rename .c files to .cpp). After fixing some problems, here are the ones that I don't know how to deal with:...
5
by: MLH | last post by:
A97 aborts creation of MDE reporting that there's a compile error in one form. Sure enough, if I remove the form from the source database and attempt to compile the MDE, it succeeds. But when I...
14
by: Urs Thuermann | last post by:
What is the most elegant way to check certain conditions at compile time? I.e. I want a compile time error to be generated if for example the size of a struct is not a multiple of 4 or if one...
5
by: Torben Laursen | last post by:
Hi Often I just want to compile one c++ file in a project the check it for errors. Now I can right click on a *.cpp file and select "Compile" But is there a way and short cut to compile a file...
2
by: BruceWho | last post by:
I downloaded boost1.35.0 and built it with following command: bjam --toolset=msvc-7.1 --variant=release --threading=multi -- link=shared --with-system stage and it failed to compile, error...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.