【发布时间】:2010-11-01 01:07:43
【问题描述】:
我最近试图衡量我的运算符重载/模板能力,作为一个小测试,我在下面创建了 Container 类。虽然此代码在 MSVC 2008(显示 11)下编译良好且正常工作,但 MinGW/GCC 和 Comeau 都因 operator+ 过载而窒息。因为我比 MSVC 更信任他们,所以我试图找出我做错了什么。
代码如下:
#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;
}
这是 Comeau 给出的错误:
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".
我很难让 Comeau/MingGW 打球,所以我求助于你们。我的大脑已经很久没有在 C++ 语法的重压下如此融化了,所以我觉得有点尴尬;)。
编辑:消除了初始 Comeau 转储中列出的(不相关的)左值错误。
【问题讨论】:
标签: c++ templates operators operator-overloading