Thread View: gnu.g++.help
2 messages
2 total messages
Started by GORDON_KEI
Mon, 23 Oct 1995 03:25
libg++ 2.7.0 String.h ambiguities
Author: GORDON_KEI
Date: Mon, 23 Oct 1995 03:25
Date: Mon, 23 Oct 1995 03:25
35 lines
1367 bytes
1367 bytes
Question about libg++ 2.7.0 String.h
The program:
#include <String.h>
#include <iostream.h>
main() {
String a = "string";
String b = "abc";
cout << a.from(b.length());
}
generates the following compiler errors:
junk2.cc: In function `int main()':
junk2.cc:8: call of overloaded method `from(unsigned int)' is ambiguous
/usr/local/lib/g++-include/String.h:277: candidates are: String::from(const String &, int)
/usr/local/lib/g++-include/String.h:276: String::from(int)
/usr/local/lib/g++-include/String.h:280: String::from(char, int)
I personally feel that the return from length() should be usable in from()
without the need for type casting. I'm not very familiar with c++, so I'm not
sure if this a funny in the standards or a bug in libg++, or just
unreasonable expectations on my part.
Any comments?
Gordon Keith
gordon_kei@antdiv.gov.au
Programmer
Marine Science Section
Australian Antarctic Division
Re: libg++ 2.7.0 String.h ambiguities
Author: Rick Stanley
Date: Mon, 23 Oct 1995 14:39
Date: Mon, 23 Oct 1995 14:39
51 lines
2451 bytes
2451 bytes
GORDON_KEI@antdiv.gov.au writes:
>Question about libg++ 2.7.0 String.h
>The program:
>#include <String.h>
>#include <iostream.h>
>main() {
> String a = "string";
> String b = "abc";
> cout << a.from(b.length());
>}
Considering that a & b, are two different strings, I can't see the value
of this example.
>generates the following compiler errors:
>junk2.cc: In function `int main()':
>junk2.cc:8: call of overloaded method `from(unsigned int)' is ambiguous
>/usr/local/lib/g++-include/String.h:277: candidates are: String::from(const String &, int)
>/usr/local/lib/g++-include/String.h:276: String::from(int)
>/usr/local/lib/g++-include/String.h:280: String::from(char, int)
>I personally feel that the return from length() should be usable in from()
>without the need for type casting. I'm not very familiar with c++, so I'm not
>sure if this a funny in the standards or a bug in libg++, or just
>unreasonable expectations on my part.
The return value from length() is an unsigned int. The function from()
that you expected to use, expects to receive an int. They are two different
data types, and the compiler could do a conversion from an unsigned int to
either an int or (in the case of the other overloaded function) a char (The
second parameter has a default argument). The compiler cannot choose between
the two, according to the rules of overloaded function matching. You must
cast the return from length() to an int, in this case, to prevent function
ambiguity.
May I also suggest reading either Stanley Lippman's "C++ Primer" or Bjarne
Stroustrup's "The C++ Progrmming Language" for more information on
function overloading, data conversion, and other topics.
--
Rick Stanley | RSI
stanleyr@acf4.nyu.edu | (212) 740-6600
Information Technologies Institute | C++ & C Language
New York University NYC, NY USA | Programming, Training, Consulting
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads