help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Default parameters - problems


From: John V. Shahid
Subject: Re: Default parameters - problems
Date: Fri, 15 Jun 2007 18:18:16 -0400

On Fri, 2007-06-15 at 21:20 +0530, anitha boyapati wrote:
> Hello,
> 
>   I faced some compilation errors with a small test case. I sort of
> accidentally hit it.
>   and since then I am trying to analyse the cause with no use. Please
> look at the code
>    below:
> 
> File - test.h
> 
>  #include <iostream>

I believe in this case "#include <string>" is more appropriate since you
are not using IO. Even if you were using IO, you have to include
<string> to make your code portable since you need the string type in
your functions and there is no guarantees that the <iostream> will
include the <string> header for you.

> using namespace std;
> class test {
>       public :
>               test() { };

I was surprised that g++ compiled your code (after removing the first
function and leaving the overloaded one) although you have a semicolon
after the function test(), which is something I've never seen before and
thought was invalid !!!

>               void func1(string str1, bool i = false, int id = -1);   
> 
>               //overload func1
>               void func1(string str1, string str2,  bool i = false);
>               };
> 
>  File - test.cc
> 
>  #include "test.h"
> 
>       void test::func1(string str1, bool i, int id ) {
>       }       
> 
>       void test::func1(string str1, string str2, bool  i ) {
>       }       
> 
>       int main() {
>               test *t = new test();
>               t->func1("hello", "world", false); // call func1
>       }
> 
> -----------------------------------------------------------------------------------------------------------
>  E:\test\g++ test.cc
> 
> test.cc: In function `int main()':
> test.cc:11: error: call of overloaded `func1(const char[6], const
> char[6], bool)' is ambiguous
> test.cc:3: note: candidates are: void test::func1(std::string, bool, int)
> test.cc:6: note:  void test::func1(std::string, std::string, bool)
>  Why is this not compiling ? Can somebody explain what is going on here ?
>  Why is it not directly going to overloaded func1 () - declared second. ?

If you looked at the first and the third argument, you'll notice that
they both satisfy the signature of both functions (i.e. the original an
the overloaded). The first argument can successfully be converted to a
string. The third argument, which is a "false" in this case, can also be
converted to its integer value '0' as described by the c++ standard. 

This leaves the second argument, which actually surprised me, that the
compiler get confused whether to convert "world" into a string or a
bool. After a while I realized that "world" is neither a string nor a
bool, instead it is a char array,  which requires the compiler to cast
it to either type, thus the candidates mentioned above. In other words,
if string(const char*) can convert the char* to a string, then certainly
static_cast<bool>() can convert the char* to a bool also. This makes the
compiler confused about which of the two functions is to be called.

A workaround to that is to call the overloaded function as follows:
t->func1("hello", string("world"), false);//This will work

>  Obviously removing the third parameter of type int from
>  void func1(string str1, bool i = false, int id = -1) succeeds in compiling.

Once you removed the third argument, the two functions don't have the
same number of arguments anymore and the compiler can successfully
decide which one to call based on the argument numbers.

good luck !
-- 
John V. Shahid <jvshahid@gmail.com>





reply via email to

[Prev in Thread] Current Thread [Next in Thread]