🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.help
Article #797

Re: libg++ 2.7.0 String.h ambiguities

#797
From: Rick Stanley
Date: Mon, 23 Oct 1995 14:39
51 lines
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
Technical Details
Message-ID: <46g9fd$l2i@cmcl2.NYU.EDU>
Path: arc1-ca.pugleaf.net!oitnews.harvard.edu!cmcl2!acf4!stanleyr
References: <951023092721.20411a5c@antdiv.gov.au>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!oitnews.harvard.edu!cmcl2!acf4!stanleyr
From: stanleyr@acf4.nyu.edu (Rick Stanley)
Newsgroups: gnu.g++.help,comp.lang.c++
Subject: Re: libg++ 2.7.0 String.h ambiguities
Date: 23 Oct 1995 14:39:41 GMT
Organization: New York University
Message-ID: <46g9fd$l2i@cmcl2.NYU.EDU>
References: <951023092721.20411a5c@antdiv.gov.au>
NNTP-Posting-Host: acf4.nyu.edu
X-Newsreader: NN version 6.5.0 #5 (NOV)