C++模板限制成员构造函数

发布于 2024-11-30 10:36:20 字数 1830 浏览 0 评论 0原文

这是我第一次涉足 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&, const T& value)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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

写下不归期 2024-12-07 10:36:20

在初始化表达式 _root(leftTree.Root(), rightTree.Root(), value) 中,_root 是一个指针。您只能将其初始化为另一个指针。也许您的意思是将其初始化为指向根据这些参数构造的新节点的指针?

这可以这样做:(在编辑后更新

_root(new BinaryTreeNode<T>(leftTree.Root(), rightTree.Root(), value))

但是,这是非常危险的(考虑分配中的异常),您应该避免在类设计中使用原始指针,而是使用智能管理指针。

同样,初始化器 _root(value) 做了错误的事情,您可能想要:(

_root(new BinaryTreeNode<T>(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)

_root(new BinaryTreeNode<T>(leftTree.Root(), rightTree.Root(), value))

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:

_root(new BinaryTreeNode<T>(value))

(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.

无戏配角 2024-12-07 10:36:20

您在两个类声明之后都错过了 ;

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))){}
};

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) {}
};

You have missed ; after both class declarations!

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))){}
};

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) {}
};
安稳善良 2024-12-07 10:36:20

我相信您需要一个 BinaryTree(); 形式的构造函数

I believe that you need a constructor in the form BinaryTree<T>();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文