public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Default parameters - problems
       [not found] <f4c211e90706150850m2271bc6fpbadb0c12e3e5ec8a@mail.gmail.com>
@ 2007-06-15 16:23 ` anitha boyapati
  2007-06-18 15:06   ` John Love-Jensen
  2007-06-15 18:19 ` Fwd: " anitha boyapati
  1 sibling, 1 reply; 3+ messages in thread
From: anitha boyapati @ 2007-06-15 16:23 UTC (permalink / raw)
  To: gcc-help

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>
using namespace std;
class test {
        public :
                test() { };
                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. ?

 Explanation  is greatly welcome ( I would be much more relieved than
 hoping and searching in bugzilla)

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

 My version : g++ 3.4.4 (cygming special)

E:test> g++ -v

Reading specs from /usr/lib/gcc/i686-pc-cygwin/3.4.4/specs
Configured with:
/usr/build/package/orig/test.respin/gcc-3.4.4-3/configure --verbose
--prefix=/usr --exec-prefix=/usr --sysconfdir=/etc --libdir=/usr/lib
--libexecdir=/usr/lib --mandir=/usr/share/man
--infodir=/usr/share/info
--enable-languages=c,ada,c++,d,f77,pascal,java,objc --enable-nls
--without-included-gettext --enable-version-specific-runtime-libs
--without-x --enable-libgcj --disable-java-awt --with-system-zlib
--enable-interpreter --disable-libgcj-debug --enable-threads=posix
--enable-java-gc=boehm --disable-win32-registry
--enable-sjlj-exceptions --enable-hash-synchronization
--enable-libstdcxx-debug
Thread model: posix
gcc version 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)



--
Anitha Boyapati


-- 
anithaboyapati

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Fwd: Default parameters - problems
       [not found] <f4c211e90706150850m2271bc6fpbadb0c12e3e5ec8a@mail.gmail.com>
  2007-06-15 16:23 ` Default parameters - problems anitha boyapati
@ 2007-06-15 18:19 ` anitha boyapati
  1 sibling, 0 replies; 3+ messages in thread
From: anitha boyapati @ 2007-06-15 18:19 UTC (permalink / raw)
  To: gcc-help

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>
using namespace std;
class test {
        public :
                test() { };
                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. ?

 Explanation  is greatly welcome ( I would be much more relieved than
 hoping and searching in bugzilla)

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

 My version : g++ 3.4.4 (cygming special)

E:test> g++ -v

Reading specs from /usr/lib/gcc/i686-pc-cygwin/3.4.4/specs
Configured with:
/usr/build/package/orig/test.respin/gcc-3.4.4-3/configure --verbose
--prefix=/usr --exec-prefix=/usr --sysconfdir=/etc --libdir=/usr/lib
--libexecdir=/usr/lib --mandir=/usr/share/man
--infodir=/usr/share/info
--enable-languages=c,ada,c++,d,f77,pascal,java,objc --enable-nls
--without-included-gettext --enable-version-specific-runtime-libs
--without-x --enable-libgcj --disable-java-awt --with-system-zlib
--enable-interpreter --disable-libgcj-debug --enable-threads=posix
--enable-java-gc=boehm --disable-win32-registry
--enable-sjlj-exceptions --enable-hash-synchronization
--enable-libstdcxx-debug
Thread model: posix
gcc version 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)



--
Anitha Boyapati


-- 
anithaboyapati

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Default parameters - problems
  2007-06-15 16:23 ` Default parameters - problems anitha boyapati
@ 2007-06-18 15:06   ` John Love-Jensen
  0 siblings, 0 replies; 3+ messages in thread
From: John Love-Jensen @ 2007-06-18 15:06 UTC (permalink / raw)
  To: anitha boyapati, MSX to GCC

Hi Anitha,

> Why is this not compiling ?

Because the code is ambiguous as to which func1 should be used.

> Can somebody explain what is going on here ?

The code is ambiguous, and the compiler cannot guess as to which func1 is
desired.

> Why is it not directly going to overloaded func1 () - declared second. ?

Because the signatures of the parameters do not match the second func1 any
better than the first func1.

There are three ways you can fix your source code:

1. change the 'string' parameters to 'const char*' parameters.

2. change the calling parameters from const char[] literals (the "hello" and
"world"), to string objects instead.

t->func1(string("hello"), string("world"), false);

3. Use different method names instead of overloading func1 with two
different sets of parameters.

Also a point of style:  don't do "using namespace" in the global scope of
header files.  (I'd even go a step further and strongly recommend never
doing "using namespace" anywhere in header files; but I'm draconian that
way.)

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-06-18 14:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <f4c211e90706150850m2271bc6fpbadb0c12e3e5ec8a@mail.gmail.com>
2007-06-15 16:23 ` Default parameters - problems anitha boyapati
2007-06-18 15:06   ` John Love-Jensen
2007-06-15 18:19 ` Fwd: " anitha boyapati

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).