gpt4 book ai didi

c++ - 无效使用非静态数据成员 ‘lasd::BinaryTreeVec::treeVec’

转载 作者:行者123 更新时间:2023-12-02 10:56:40 25 4
gpt4 key购买 nike

我正在使用我之前定义的 vector 来实现二叉树。二叉树内部有一个结构节点,就像无法从我的节点函数访问在二叉树类中定义的 vector 一样。

所以这是二叉树:

template <typename Data>
class BinaryTreeVec : public BinaryTree<Data>{
private:

protected:
using BinaryTree<Data>::size;
ulong height = 0;

public:
using typename BinaryTree<Data>::Node;

struct NodeVec : public Node{
private:

protected:
using Node::value;
ulong left;
ulong right;
ulong index;
ulong height;
bool isValid = false;

public:
friend class BinaryTreeVec<Data>;

....


bool HasLeftChild() const noexcept override; // Override Node member
bool HasRightChild() const noexcept override; // Override Node member

....
}

....

protected:
Vector<struct NodeVec> treeVec;
}

一切正常,直到我调用 HasLeftChild() 函数
错误:非静态数据成员“lasd::BinaryTreeVec::treeVec”的使用无效。
我的教授建议我使用引用是解决问题的最佳选择,因此尝试声明对 的引用树 vector 这样我就可以在 NodeVec 中使用它,但它完全没用。
template <typename Data>
bool BinaryTreeVec<Data>::NodeVec::HasLeftChild() const noexcept{
if( 2 * index + 1 < treeVec.Size())
return ( treeVec[2 * index + 1].flag == true );

return false;
}

每次使用 时都会出现编译器错误树 vector 在这里的 HasLeftChild() 函数中。

最佳答案

以下是如何将 treeVec 引用添加到您的节点类

struct NodeVec : public Node {
protected:
...
Vector<NodeVec>& treeVec;
public:
NodeVec(Vector<NodeVec>& tv) : treeVec(tv) {}
...
};

我创建了一个新的构造函数来绑定(bind)引用。我假设您还没有构造函数,但如果有,则必须将上述代码添加到现有构造函数中。

当然,您需要更改现有代码,无论您在何处创建节点以传入新的 treevec 参数。

关于c++ - 无效使用非静态数据成员 ‘lasd::BinaryTreeVec<int>::treeVec’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61618904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com