Reputation: 1555
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.
Upvotes: 6
Views: 6833
Reputation: 32635
You'd be better off defining the function directly in the class. Also, you should pass the parameters as const
references.
template <typename T>
class Container
{
public:
friend Container operator+ (Container const & lhs, Container const & rhs)
{
// ...
}
};
Upvotes: 1
Reputation: 7954
I gave this a shot under GCC and got it to compile and run with a few changes. There were two changes I had to make to get GCC happy.
One was the declaration of the friend template function. It is its own template declaration separate from the class one, so I used U instead of the Container class' T there. I also got rid of the <> after the operator+. I don't think you'd need those unless you're writing a template specialization.
template<typename U>
friend Container<U> operator+ (Container<U>& lhs, Container<U>& rhs);
Two, the line
Container<int>& c = a + b;
didn't fly with GCC because you're asking to store a reference to a temporary (the result of the addition). I removed the ampersand so that there is a place to store the result.
I saw your post just now, that also works because of the forward declarations. I guess I'll post this anyway as an alternative that doesn't require them. Of course I only tested in GCC...
Upvotes: 1
Reputation: 103515
'operator+' is not a member function, and it's not templated. It's just the operator+ which takes templated parameters.'
template <typename T>
Container<T> operator+ (Container<T>& lhs, Container<T>& rhs)
Upvotes: 0
Reputation: 229603
template <typename T>
Container<T> operator+ <> (Container<T>& lhs, Container<T>& rhs)
Here the "<>" after operator+
should be removed since you're just declaring a new template, not specializing a general one. Also at least g++
wants to see the template declaration before the friend declaration, so it needs to be moved before the declaration of Container
. So the following order of the declarations works:
// forward declaration of Container<T>
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);
...
};
Upvotes: 2
Reputation: 1555
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;
}
Upvotes: 6