【问题标题】:Binary tree function does not work with templates二叉树函数不适用于模板
【发布时间】:2015-01-04 15:33:08
【问题描述】:

您好,如果您能帮助我理解为什么会发生以下错误以及如何解决它,我会很高兴,该错误是二叉树的一部分。

谢谢!!

错误-

错误 C2955 'BSNode':使用类模板需要模板参数列表

部分代码-

template <class T>
BSNode& BSNode::operator=(const BSNode& other)
{
_data = other._data;
_left = 0;
_right = 0;

//deep recursive copy
if (other._left)
{
    _left = new BSNode(*other._left);
}

if (other._right)
{
    _right = new BSNode(*other._right);
}

return *this;
} 

【问题讨论】:

    标签: c++ templates compiler-errors binary-tree


    【解决方案1】:

    正如消息所说,您需要提供模板参数:

    BSNode<T>& ... const BSNode<T>& other ...
    

    编译器可能会推断出两个构造函数调用的参数。如果不是这样,您也需要为它们提供参数。

    顺便说一句,模板通常在其头文件中实现,因为通常很难或不可能将其声明与实现分开。

    【讨论】:

    • 但是现在在这个更正之后我得到了以下错误“期望一个类或命名空间”怎么办?
    • @yronmosh 你能发布更多代码吗?也许这会帮助别人理解你为什么会得到这个错误?也许您没有使用命名空间限定模板参数?
    猜你喜欢
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    相关资源
    最近更新 更多