我有以下代码:
template<typename E, typename M, bool treeIsLeft>
class Digit ... {
...
friend std::ostream& operator <<(std::ostream& os, Digit<E, M, true> const& d){
...
}
friend std::ostream& operator <<(std::ostream& os, Digit<E, M, false> const& d){
...
}
}
使用Clang编译时,它可以正常工作,但是使用g ++ 4.9我会得到两个错误
../src/fingertree.h:399:26: error: redefinition of 'std::ostream& operator<<(std::ostream&, const Digit<std::basic_string<char>, int, true>&)'
friend std::ostream& operator <<(std::ostream& os, Digit<E, M, true> const& d){
^
../src/fingertree.h:399:26: note: 'std::ostream& operator<<(std::ostream&, const Digit<std::basic_string<char>, int, true>&)' previously defined here
../src/fingertree.h:402:26: error: redefinition of 'std::ostream& operator<<(std::ostream&, const Digit<std::basic_string<char>, int, false>&)'
friend std::ostream& operator <<(std::ostream& os, Digit<E, M, false> const& d){
^
../src/fingertree.h:402:26: note: 'std::ostream& operator<<(std::ostream&, const Digit<std::basic_string<char>, int, false>&)' previously defined here
我做错了吗?如何使用g ++?
编辑: 我发现了如何使它工作:我必须将定义移到类体外,并仅保留其中的声明。我还要插入模板&lt; ...&gt;符。然而,这感觉不必要的冗长和不自然,我仍然想知道在课堂上定义朋友有什么不妥。