Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 233389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:05:48+00:00 2026-05-11T20:05:48+00:00

I was recently trying to gauge my operator overloading/template abilities and as a small

  • 0

I was recently trying to gauge my operator overloading/template abilities and as a small test, created the Container class below. While this code compiles fine and works correctly under MSVC 2008 (displays 11), both MinGW/GCC and Comeau choke on the operator+ overload. As I trust them more than MSVC, I’m trying to figure out what I’m doing wrong.

Here is the code:

#include <iostream>

using namespace std;

template <typename T>
class Container
{
      friend Container<T> operator+ <> (Container<T>& lhs, Container<T>& rhs);
   public: void setobj(T ob);
     T getobj();
      private: T obj;
};

template <typename T>
void Container<T>::setobj(T ob)
{
   obj = ob;
}

template <typename T>
T Container<T>::getobj()
{
   return obj;
}

template <typename T>
Container<T> operator+ <> (Container<T>& lhs, Container<T>& rhs)
{
      Container<T> temp;
      temp.obj = lhs.obj + rhs.obj;
      return temp;
}

int main()
{    
    Container<int> a, b;

 a.setobj(5);
    b.setobj(6);

 Container<int> c = a + b;

 cout << c.getobj() << endl;

    return 0;
}

This is the error Comeau gives:

Comeau C/C++ 4.3.10.1 (Oct  6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing.  All rights reserved.
MODE:strict errors C++ C++0x_extensions

"ComeauTest.c", line 27: error: an explicit template argument list is not allowed
          on this declaration
  Container<T> operator+ <> (Container<T>& lhs, Container<T>& rhs)
               ^

1 error detected in the compilation of "ComeauTest.c".

I’m having a hard time trying to get Comeau/MingGW to play ball, so that’s where I turn to you guys. It’s been a long time since my brain has melted this much under the weight of C++ syntax, so I feel a little embarrassed ;).

EDIT: Eliminated an (irrelevant) lvalue error listed in initial Comeau dump.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:05:48+00:00Added an answer on May 11, 2026 at 8:05 pm

    I found the solution thanks to this forum posting. Essentially, you need to have a function prototype before you can use ‘friend’ on it within the class, however you also need the class to be declared in order to properly define the function prototype. Therefore, the solution is to have two prototype definitons (of the function and the class) at the top. The following code compiles under all three compilers:

    #include <iostream>
    
    using namespace std;
    
    //added lines below
    template<typename T> class Container;
    template<typename T> Container<T> operator+ (Container<T>& lhs, Container<T>& rhs); 
    
    template <typename T>
    class Container
    {
          friend Container<T> operator+ <> (Container<T>& lhs, Container<T>& rhs);
          public: void setobj(T ob);
                  T getobj();
          private: T obj;
    };
    
    template <typename T>
    void Container<T>::setobj(T ob)
    {
          obj = ob;
    }
    
    template <typename T>
    T Container<T>::getobj()
    {
          return obj;
    }
    
    template <typename T>
    Container<T> operator+ (Container<T>& lhs, Container<T>& rhs)
    {
          Container<T> temp;
          temp.obj = lhs.obj + rhs.obj;
          return temp;
    }
    
    int main()
    {    
        Container<int> a, b;
    
        a.setobj(5);
        b.setobj(6);
    
        Container<int> c = a + b;
    
        cout << c.getobj() << endl;
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've become very addicted to Project Euler recently and am trying to do this
recently I downloaded this open source project and I am trying to compile it.
I recently came across this in some code - basically someone trying to create
Recently, my Eclipse 3.4 installation started complaining while trying to update installed software. I
I was recently trying to assert the inequality in one of the test. However
I want to share this odd but interesting situation I stumbled upon recently while
recently I was trying to do something like this in WP7 app I have
I was recently trying to do a project*, which caused me to ask this
Recently I've been trying to adapt Selenium 1 test cases designed to work in
I recently encountered some problems while trying to sort a set of objects. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.