C++模板限制成员构造函数
这是我第一次涉足 C++ 模板,我正在尝试构建一个 BinaryTree
模板来帮助我解决 Project Euler 问题;但是,我似乎收到一个错误,其中 BinaryTree
类无法识别 BinaryTreeNode
的所有构造函数!这是代码片段。
template <class T>
class BinaryTreeNode
{
private:
BinaryTreeNode<T>* _left;
BinaryTreeNode<T>* _right;
T* _value;
public:
BinaryTreeNode();
explicit BinaryTreeNode(const T& value) : _value(&(T(value))) {}
BinaryTreeNode(BinaryTreeNode<T>& left, BinaryTreeNode<T>& right, const T& value) :
_left(&left), _right(&right), _value(&(T(value))){}
};
BinaryTree 类
#include "BinaryTreeNode.h"
template <class T>
class BinaryTree
{
private:
BinaryTreeNode<T>* _root;
BinaryTreeNode<T>* _current;
unsigned int size;
public:
BinaryTree() : size(0), _root(0), _current(0) { }
explicit BinaryTree(BinaryTree<T>& leftTree, BinaryTree<T>& rightTree, const T& value) :
size(leftTree.Size() + rightTree.Size() + 1), _root(leftTree.Root(), rightTree.Root(), value), _current(_root) {}
explicit BinaryTree(const T& value) : size(1), _root(value) {}
const BinaryTreeNode<T>& Root() const { return *_root;}
};
我收到这些错误。
error C2359: 'BinaryTree<T>::_root' : member of non-class type requires single initializer expression
error C2440: 'initializing' : cannot convert from 'const int' to 'BinaryTreeNode<T> *'
error C2439: 'BinaryTree<T>::_root' : member could not be initialized
当我将其包含在主代码中时,(BinaryTreeNode
的 BinaryTreeNode
构造函数可以工作,但它似乎不适用于我的 BinaryTree
模板。有人知道为什么吗?
This is my first foray into C++ templates, and I'm trying to construct a BinaryTree
template to help me with a Project Euler problem; however, I seem to be getting an error where BinaryTree
class doesn't recognize all the constructors of the BinaryTreeNode
! Here's a snippet of the code.
template <class T>
class BinaryTreeNode
{
private:
BinaryTreeNode<T>* _left;
BinaryTreeNode<T>* _right;
T* _value;
public:
BinaryTreeNode();
explicit BinaryTreeNode(const T& value) : _value(&(T(value))) {}
BinaryTreeNode(BinaryTreeNode<T>& left, BinaryTreeNode<T>& right, const T& value) :
_left(&left), _right(&right), _value(&(T(value))){}
};
The BinaryTree class
#include "BinaryTreeNode.h"
template <class T>
class BinaryTree
{
private:
BinaryTreeNode<T>* _root;
BinaryTreeNode<T>* _current;
unsigned int size;
public:
BinaryTree() : size(0), _root(0), _current(0) { }
explicit BinaryTree(BinaryTree<T>& leftTree, BinaryTree<T>& rightTree, const T& value) :
size(leftTree.Size() + rightTree.Size() + 1), _root(leftTree.Root(), rightTree.Root(), value), _current(_root) {}
explicit BinaryTree(const T& value) : size(1), _root(value) {}
const BinaryTreeNode<T>& Root() const { return *_root;}
};
I'm getting these errors.
error C2359: 'BinaryTree<T>::_root' : member of non-class type requires single initializer expression
error C2440: 'initializing' : cannot convert from 'const int' to 'BinaryTreeNode<T> *'
error C2439: 'BinaryTree<T>::_root' : member could not be initialized
The BinaryTreeNode
constructor of (BinaryTreeNode<T>&, BinaryTreeNode<T>&, const T& value)
works when I include it in my main code, but it doesn't seem to work under my BinaryTree
template. Anyone know why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在初始化表达式
_root(leftTree.Root(), rightTree.Root(), value)
中,_root
是一个指针。您只能将其初始化为另一个指针。也许您的意思是将其初始化为指向根据这些参数构造的新节点的指针?这可以这样做:(在编辑后更新)
但是,这是非常危险的(考虑分配中的异常),您应该避免在类设计中使用原始指针,而是使用智能管理指针。
同样,初始化器
_root(value)
做了错误的事情,您可能想要:(另请注意,您应该按照声明的顺序初始化成员。)
更新:我更改了编辑后的第一个构造函数调用,但正如 @Luc 所说,您的构造函数采用非常量参数,但
Root()
仅提供 const 引用,因此您仍然需要修复该问题。In your initialization expression
_root(leftTree.Root(), rightTree.Root(), value)
,_root
is a pointer. You can only initialize it to another pointer. Perhaps you mean to initialize it to a pointer to a new node constructed on those arguments?This could be done like this: (updated after your edit)
However, this is very dangerous (think about an exception in the allocation), and you should probably avoid using raw pointers in your class design and instead use smart managing pointers.
Similarly, the initializer
_root(value)
does the wrong thing, you might want:(Also note that you should initialize members in their order of declaration.)
Update: I changed the first constructor call following your edit, but as @Luc says, your constructors take non-const arguments but
Root()
only provides a const reference, so you still need to fix that.您在两个类声明之后都错过了
;
!You have missed
;
after both class declarations!我相信您需要一个
BinaryTree();
形式的构造函数I believe that you need a constructor in the form
BinaryTree<T>();