template/operator overloading ambigous on gcc

This is a discussion on template/operator overloading ambigous on gcc within the C++ Programming forums, part of the General Programming Boards category; New 3.4.4 all-mighty gcc fails on this one Code: #include <iostream> class A{ public: A(){} }; A& operator+=(A& a,char b){return ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    722

    template/operator overloading ambigous on gcc

    New 3.4.4 all-mighty gcc fails on this one
    Code:
    #include <iostream>
    
    class A{
    public:
    	A(){}
    };
    A& operator+=(A& a,char b){return a;}
    template<class W> A operator+ (const A&,const W& b){ return A(a)+=b; }
    
    int main(){
    	A a;
    	A b = a+'h';
    	return 0;
    }
    Code:
    $ make
    g++ -Wall -c ./main.cpp -o obj/./main.o
    ./main.cpp: In function `A operator+(const A&, const W&) [with W = char]':
    ./main.cpp:12:   instantiated from here
    ./main.cpp:8: error: no match for 'operator+=' in 'A(((const A&)(+a))) += b'
    ./main.cpp:7: note: candidates are: A& operator+=(A&, char)
    make: *** [obj/./main.o] Error 1
    Yet, this source compiles great on MSVC++ 6 and 7...
    I'm thinking in submiting this code to gcc bugzilla
    Last edited by xErath; 06-16-2005 at 07:56 PM.

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    after serious code consideration on this I tried the following...
    Code:
    template<class W> A operator+ (const A& a,const W& b){ A c(a);return c+=b; }
    Now it worked... gcc wasn't passing the A(a) to the operator... yet if I declare it as local, it works... gcc is confusing implicit argument declaration with the creation of a local var, which is returned after.
    any comments now that I figured this out ?
    Last edited by xErath; 06-16-2005 at 08:07 PM.

  3. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Code:
    template<class W> A operator+ (const A&,const W& b){ return A(a)+=b; }
    1. The problem I see with this is that you don't have a variable for the first parameter, only a type, which is ok for a declaration, but not a definition.

    Code:
    template<class W> A operator+ (const A& a,const W& b){ return A(a)+=b; }
    works but usless since operator+ doesnt use b at all, but i imagine thats coming.

    Why not template both functions, as you have it now, your template will only work with char, and you will have to make a operator+ for every type you want to use it on.
    Last edited by Darryl; 06-16-2005 at 08:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  2. Replies: 4
    Last Post: 09-02-2007, 09:47 PM
  3. Compiles on gcc 3.3 but not on gcc 4.0.3
    By cunnus88 in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2007, 01:24 PM
  4. gcc not for C++?
    By tin in forum C++ Programming
    Replies: 4
    Last Post: 09-15-2004, 09:26 AM
  5. gcc
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-22-2003, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21