c++ - 关于 C++ 中模板上重载运算符的一些编译器错误

标签 c++ templates function operator-overloading friend

我有一些代码有一些错误,我根本不知道如何修复。我已经问过我的教授和助教,除了更准确地理解错误的含义之外,还没有运气地咨询了互联网。据我所知,编译器要么将我的重载运算符与内置运算符混淆,要么根本不将其识别为重载运算符。

我收到以下错误和警告:

||=== project 4, Debug ===|
\project 4\forest.h|13|warning: friend declaration 'Forest<NODETYPE>& operator+(Forest<NODETYPE>&, Forest<NODETYPE>&)' declares a non-template function|

\project 4\forest.h|13|note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) |

\project 4\forest.h|14|warning: friend declaration 'std::ostream& operator<<(std::ostream&, const Forest<NODETYPE>&)' declares a non-template function|

\project 4\forest.h|15|warning: friend declaration 'std::istream& operator>>(std::istream&, Forest<NODETYPE>&)' declares a non-template function|

\project 4\main.cpp||In function 'int main()':|
\project 4\main.cpp|21|error: ambiguous overload for 'operator>>' in 'file >> intForest'|

c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|119|note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>& (*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] <near match>|

c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|123|note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] <near match>|

c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\istream|130|note:                 std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>] <near match>|
||=== Build finished: 1 errors, 3 warnings ===|

当我尝试编译我的代码时。相关代码段如下:

(在 forest.h 中)

template< typename NODETYPE > class Forest
{

    public:
        friend Forest<NODETYPE>& operator+(Forest<NODETYPE>&, Forest<NODETYPE>&);
        friend ostream& operator<<(ostream&, const Forest<NODETYPE>&);
        friend istream& operator>>(istream&, Forest<NODETYPE>&);

        Forest();
        Forest( const Forest& otherForest);
        ~Forest();
        void nodes(int&) const;

    private:
        ForestNode<NODETYPE> *root;

        ForestNode<NODETYPE> *getNewNode( const NODETYPE &);
};

(在 forest.cpp 中)

template<typename NODETYPE> istream& operator>>(istream& file, const Forest<NODETYPE>& f1)
{
    istream file2 = file;
    int nodeCount = 0;
    string blah = ' ';

    while(getline(file2,blah))
    {
        nodeCount++;
    }

    ForestNode<NODETYPE> *forestNodeArray[nodeCount];//holds pointers to last node of depth i

    getline(file,*f1.root.tag);
    forestNodeArray[0] = &(*f1.root);
    inputHelper(file, 0, *f1.root, forestNodeArray, nodeCount);

    return file;
}

(最后,在 main.cpp 中)

int main()
{

    Forest < char > intForest();
    filebuf fb;
    fb.open ("forest1.txt",ios::in);
    istream file(&fb);


    cout << typeid(intForest).name() << endl;
    cout << typeid(file).name() << endl;

    file >> intForest;

    fb.close();


}

如有任何帮助,我们将不胜感激。

编辑:感谢 alex 和 alf,我明白了为什么不将它们视为模板函数。回想起来很明显,我只是对那些签名感兴趣。无论如何,我仍然得到关于模糊运算符的错误。为什么编译器不识别我的运算符并使用它,而不是尝试在 3 个内置版本的运算符之间做出决定,这些版本不可能有一个参数作为 Forest?

最佳答案

第二个错误在这一行:

Forest < char > intForest();

起初这可能令人惊讶,但该行并未声明 Forest<char> 类型的变量,而是一个不带参数并返回 Forest<char> 的函数. 只需从声明中删除括号:

Forest < char > intForest;

关于第一个警告,这已经解释过了(声明为 friend 的函数不是模板,这意味着您必须手动为用于实例化 Forest<> 的每种类型实现它(可能您不t 想要那个)。另请注意,声明模板化的 operator+ 然后使模板成为@Alf P. Steinbach 答案中的 friend 意味着 Forest<int> 将成为 Forest<double> 的 friend ,这可能不是什么你需要。评论中的@Alex 建议只会使模板的特定实例成为 friend ,这可能更接近你想要的,但你需要在模板类之前声明模板化运算符,为此你需要转发声明模板类...

模板中无友元函数的常见模式是就地定义函数:

template <typename T>
class Forest {
   // ...
   friend Forest& operator+( Forest const & lhs, Forest const & rhs ) [1]
   {
       // implementation here
   }
}
// [1] Note that the arguments are const references (read only), and also note that
//     you do not need to provide the type argument inside the template declaration

这允许您将其定义为非模板函数,同时让编译器为您实例化该函数。此外,在处理模板时,在类定义中内联定义成员方法通常更简单。它让生活变得更简单,毕竟在大多数情况下,您确实需要在(相同的)头文件中提供实现。

然而,在定义二元运算符时,更好的方法是定义 operator+=作为成员方法,然后你可以很容易地定义operator+作为非 friend 免费功能。模式将是:

struct test {
   test& operator+=( test const & );
};
test operator+( test lhs, test const & rhs ) { // [2]
   lhs += rhs;
   return lhs;
}
// [2] Note that the first argument is by value, and the second by const reference
//     This means that the compiler will copy the first argument for you, and you
//     are free to modify it with the += operator and return the copy.

现在,最棘手的部分是混合前两个建议。能够定义 operator+这是模板定义中的一个自由函数,一个常见的技巧是使它成为 friend ,即使出于访问原因不需要这样做:

template <typename T>
class Forest {
   Forest& operator+=( Forest const & ) {
      // implemenation
   }
   friend Forest operator+( Forest lhs, Forest const & rhs ) { // [3]
      return lhs+=rhs;
   }
};
// [3] friendship only to allow us to define the free function inside the template
//     declaration.

关于c++ - 关于 C++ 中模板上重载运算符的一些编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4039979/

相关文章:

c++ - 如何修复 cpprestsdk 中的 "error in ssl handshake"?

c++ - Concepts-Lite iterator comparison 比较值

c++ - 从派生类访问基类中的类型别名

从 main() 调用函数

javascript - 我如何实现像 jQuery 那样的链接模式?

javascript - 在不重新加载页面的情况下重置 innerHTML

c++ - 嵌套模板 gcc 编译器 4.1.2 错误

c++ - 在函数中传递 const 对象

templates - 谷歌计算引擎: How to change instance template

c++ - 继承模板类的虚拟析构函数