473,504 Members | 13,621 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why do i get an error for this ?

Hi,

Why do I get this error ?
'CRectangle::CRectangle' : ambiguous call to overloaded function

Thanks in advance.
#include <iostream.h>

class CRectangle {
int width, height;
float fw, fh;
public:
CRectangle ();
CRectangle (int,int);
CRectangle (float, float);
int area (void) {return (width*height);}
float area1(void) {return (fw * fh); }
};

CRectangle::CRectangle () {
width = 5;
height = 5;
}

CRectangle::CRectangle (int a, int b) {
width = a;
height = b;
}

CRectangle::CRectangle (float a, float b) {
fw = a;
fh = b;
}

int main () {
CRectangle rect (3,4);
CRectangle rectb;
CRectangle rectf(2.5, 2.5);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
cout << "rectf area" << rectf.area1() << endl;
return 0;
}

Jul 22 '05 #1
8 1575
<co*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi,

Why do I get this error ?
'CRectangle::CRectangle' : ambiguous call to overloaded function
See below.
Thanks in advance.
#include <iostream.h>
#include <iostream>
#include <ostream>

using std::cout;
using std::endl;
class CRectangle {
int width, height;
float fw, fh;
public:
CRectangle ();
CRectangle (int,int);
CRectangle (float, float);
int area (void) {return (width*height);}
float area1(void) {return (fw * fh); }
};

CRectangle::CRectangle () {
width = 5;
height = 5;
}

CRectangle::CRectangle (int a, int b) {
width = a;
height = b;
}

CRectangle::CRectangle (float a, float b) {
fw = a;
fh = b;
}

int main () {
CRectangle rect (3,4);
CRectangle rectb;
CRectangle rectf(2.5, 2.5);
This is the problem line (you should have indicated that in your post).
The reason is that the literal 2.5 has type 'double', there's no
constructor that takes that type, so it cannot determine if you
want those values cast to 'int', or 'float' (the only type arguments
you provided constructors for). The solution is to tell it which to
use:

CRectangle rectf(2.5f, 2.5f); /* the 'f' suffix denotes type 'float' */

(Anyway, you should be using type 'double' instead of 'float'
for your floating point values, unless you have a compelling
reason to do otherwise).
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
cout << "rectf area" << rectf.area1() << endl;
return 0;
}


-Mike

Jul 22 '05 #2
co*******@gmail.com wrote:
Why do I get this error ?
'CRectangle::CRectangle' : ambiguous call to overloaded function

Thanks in advance.
#include <iostream.h>

class CRectangle {
int width, height;
float fw, fh;
public:
CRectangle ();
CRectangle (int,int);
CRectangle (float, float);
int area (void) {return (width*height);}
float area1(void) {return (fw * fh); }
};

CRectangle::CRectangle () {
width = 5;
height = 5;
}

CRectangle::CRectangle (int a, int b) {
width = a;
height = b;
}

CRectangle::CRectangle (float a, float b) {
fw = a;
fh = b;
}

int main () {
CRectangle rect (3,4);
CRectangle rectb;
CRectangle rectf(2.5, 2.5);
"2.5" is a floating point literal of type 'double'. The compiler
cannot decide whether to make 'float' of them or 'int'. You have
to help it. If you intended to instantiate CRectangle using its
constructor that accepts 'float' arguments, pass float:

CRectangle rectf(2.5f, 2.5f);
[..]


Also, it may not be very important in your particular exercise, but
your CRectangle class is not very well designed. The main problem I
see is that I can instantiate a CRectangle using integers and still
call 'area1', which will use _uninitialised_ data members fw and fh,
which can cause CPU to throw a shoe. Perhaps you should look at making
it a template.

V
Jul 22 '05 #3
co*******@gmail.com wrote:

Hi,

Why do I get this error ?
'CRectangle::CRectangle' : ambiguous call to overloaded function


Because the compiler has 2 ways to deal
with construction

In
CRectangle rect(2.5, 2.5);
both numbers are of type double, so you force the
compiler to decide if it takes constructor
CRectangle( int, int );
or it uses constructor
CRectangle( float, float);

Neither constructor matches your argument types (which
are of type double), but the arguments could be converted
to any one of the required data types (int or float).
Both are consider to be equal good matches, thus the
compiler decides to let you clearify.

Best thing:
Throw away the constructor which takes only int.
You don't need it. The one which takes 2 floats
does the very same thing.

Next best thing:
Clearify which constructor you want, by writing
CRectangle rect( 2.5f, 2.5f);
But this leaves another problem. Suppose you want to
write:
CRectangle rect( 2, 3.1415f);
So which constructor should be taken? The one that takes
2 int or the one which takes 2 float? Solution: see
solution one, throw away the constructor taking int.

Also: *** Don't use float ***
Never!
That is: Until you know what you do, have the knowledge
to fight that beast, are willing to fight that beast and
you don't have a very, very, very good reason, always use
data type double and forget that float even exists.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4
Thanks folks. (For all your thoughts).
I understand constructor cannot accept "float". - By design ?
Your response has created another question in my mind.
Why would I use "double" rather than "float" ? My understanding is
"double" consumes the memory than that of "float" ?

Thanks.
Karl Heinz Buchegger wrote:
co*******@gmail.com wrote:

Hi,

Why do I get this error ?
'CRectangle::CRectangle' : ambiguous call to overloaded function


Because the compiler has 2 ways to deal
with construction

In
CRectangle rect(2.5, 2.5);
both numbers are of type double, so you force the
compiler to decide if it takes constructor
CRectangle( int, int );
or it uses constructor
CRectangle( float, float);

Neither constructor matches your argument types (which
are of type double), but the arguments could be converted
to any one of the required data types (int or float).
Both are consider to be equal good matches, thus the
compiler decides to let you clearify.

Best thing:
Throw away the constructor which takes only int.
You don't need it. The one which takes 2 floats
does the very same thing.

Next best thing:
Clearify which constructor you want, by writing
CRectangle rect( 2.5f, 2.5f);
But this leaves another problem. Suppose you want to
write:
CRectangle rect( 2, 3.1415f);
So which constructor should be taken? The one that takes
2 int or the one which takes 2 float? Solution: see
solution one, throw away the constructor taking int.

Also: *** Don't use float ***
Never!
That is: Until you know what you do, have the knowledge
to fight that beast, are willing to fight that beast and
you don't have a very, very, very good reason, always use
data type double and forget that float even exists.

--
Karl Heinz Buchegger
kb******@gascad.at


Jul 22 '05 #5
co*******@gmail.com wrote:
Thanks folks. (For all your thoughts).
I understand constructor cannot accept "float". - By design ?
Why can't it? How do you derive that from our responses?
Your response has created another question in my mind.
Why would I use "double" rather than "float" ? My understanding is
"double" consumes the memory than that of "float" ?


I take it that you wanted to write <"double" consumes more memory>.

Yes, it can. On your system it probably does. Are you running out of
memory? Until you do (and very likely you never will), do not concern
yourself with using smaller data types just for the sake of using less
memory.

And, please don't top-post. Thanks.

Victor
Jul 22 '05 #6
<co*******@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Thanks folks. (For all your thoughts).
I understand constructor cannot accept "float". - By design ?
Yes, one of your constructors can indeed accept arguments
of type 'float'. The problem was that you did not supply
any arguments of that type.
Your response has created another question in my mind.
Why would I use "double" rather than "float" ?
Because it's guaranteed to have a much larger range
and precision than type 'float'.
My understanding is
"double" consumes the memory than that of "float" ?


It might, it might not, this depends upon the implementation.
Nothing prevents it from having the same range and precision
as 'double'. BUT: its required range and precision are much less.

Even if 'double' does consume more memory than 'float',
does this really matter? How much of a difference does
it really make for your program, and is this difference
really a problem?

BTW please don't top-post.

-Mike
Jul 22 '05 #7
co*******@gmail.com wrote:

Thanks folks. (For all your thoughts).
I understand constructor cannot accept "float". - By design ?
Your response has created another question in my mind.
Why would I use "double" rather than "float" ? My understanding is
"double" consumes the memory than that of "float" ?


Granted. On the other hand double has more precision then float.
What do you prefer: saving memory or running the risc of wrong
results (*)

Try the following program:

#include <iostream>
#include <iomanip>
using namespace std;

int main () {
float a = 12345.12;
float b = 12345.12;

cout << setprecision(15) << a * b << endl;
return 0;
}

When I run it on my computer the output is
152401984

but when I power up my pocket calculator, it tells me that
the correct answer is:
152401987.8144

(Note: Even the immediate digit left of comma is already wrong. 4 versus 7)

See the difference it makes now? (you might try to change float
to double in the above and retest the whole thing).
(*) A similar thing is true for double too. But since double typically has
a lot more precision then float, the 'error' is much smaller.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #8
> Why would I use "double" rather than "float" ? My understanding is
"double" consumes the memory than that of "float" ?


Yes, a double typically consumes more memory. Some computations with
doubles are also marginally slower than with floats.

Unfortunately, a typical float has _such_ limited precision (around six
significant digits at best) that unless you know a _lot_ about
numerical analysis, there's a very strong chance that by the time you
finish an arbitrarily chosen algorithm with floats, you may easily have
only one or two digits left that mean anything -- and there are quite a
few algorithms that can destroy it all, so you get answers that
literally don't mean anythng at all.

Now, it's _possible_ for the same thing to happen with double's as
well, but a double has enough more precision to start with (typically
around 15 digits or so) that it's much easier to maintain sufficient
accuracy.

There was a time, of course,when memory was quite expensive: in 1964
the monthly rent on a 12Kbyte memory module ran over $500US/month. At
that rate, it was well worth spending time and effort to make your
calculations work in a smaller data type. (see:
http://ed-thelen.org/comp-hist/BRL64-p.html for one example of
historical pricing, if you care).

That's not the case anymore though -- memory is cheap. If you know (for
example) that you're going to be storing 100 million of them, it may be
worth the extra trouble to use a float, but otherwise, there's unlikely
to be much reason to mess with it.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4563
by: Andrew Luke | last post by:
Hi all you C++ guru's! I'm 'very, very' new to C++ and I'm having a little trouble configuring my VS environment I think - when I try and compile some sample code I'm getting the following...
1
7458
by: Minh | last post by:
I've just installed VS.NET 2003 on my Athlon XP 1800+. However I couldn't get any project with STL includes to compile even if I create a new empty project (and added #include <string>). It gave me...
3
16191
by: Manuel | last post by:
I'm trying to compile glut 3.7.6 (dowbloaded from official site)using devc++. So I've imported the glut32.dsp into devc++, included manually some headers, and start to compile. It return a very...
4
6386
by: Sanjay Kumar | last post by:
Folks ! I am working with VC++ after a long time and having problem linking latest xerces 2.7 in VC++ 2005 Express Edition. I have done following: 1. downloaded and unpacked the the...
1
3264
by: developer | last post by:
Hi All I have made a .NET project. the files included are borland c++ files that i am migrate to VC++ .NET I am using Microsoft Visual C++ .NET 2003. the compilation goes through properly,...
9
13242
by: Prasad | last post by:
HI, I am a beginner in VC++.. I am trying to write a Win32 console application in visual studio.. I am using following header files.. #include <STRING> using namespace std; #include...
0
4716
by: mchuc7719 | last post by:
Hello, I have a Vb.Net 2005 ClassLibrary, when I try to compile using MSBee, only get errors. Before I to run the command line, I open in notepad the .vbproj and I was add the next line: ...
2
3909
by: Nick | last post by:
I'm learning C++ and ran into a compile error using Visual C++ 2005 Express on the following example program (located at http://www.cplusplus.com/doc/tutorial/templates.html): // template...
2
5633
by: khalidanwar123 | last post by:
i am getting the following error while updating a clob field. ERROR java.sql.SQLException: Data size bigger than max size forthis type: 4003 19:28:27,499 ERROR at...
15
5662
by: madhu.ab | last post by:
Hi All, I am getting the following errors when i am including header file winuser.h I dont know whats happening. How will an error occur in winuser.h?? Please help. \microsoft visual...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1526
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
406
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.