【发布时间】:2016-11-17 22:44:59
【问题描述】:
我正在尝试为我的二叉搜索树创建一个递归插入函数。但是,我不断收到以下错误“无法获取 Node* 类型的右值的地址”。我知道如果我的指针为 NULL,那么获取它的地址会给我这个错误,但是我将如何解决这个问题呢?
注意:Node(data* _data) 构造函数在创建对象时将左右指针设置为 NULL。
这是我的代码,我已经注释了产生右值错误的两行。我也在使用 Xcode。
提前致谢。
class Node {
private:
data* data;
Node* left;
Node* right;
public:
Node(data* _data);
~Node();
Node* getLeft(); // gets the left immediate descendent of the Node.
Node* getRight(); // gets the right immediate descendent of the Node
data* getVal(); //returns pointer to data
};
class Tree {
private:
Node* root;
int nodeCount;
Node* placeNode(Node** root, data* data);
public:
Tree();
~Tree();
bool placeIn(data* newData); //placeIns node into ordered BST
};
bool Tree::placeIn(data* newData) {
return placeNode(root, newData); //placeIns newData object into BST
}
Node* Tree::placeNode(Node** root, data* data) {
//placeIns node into BST via in order traversal
if ((*root) == NULL) {
return (*root) = new Node(data);
}
if ((*root)->getVal()->getName() == data->getName()) {
cout << "Node exists" << endl;
return NULL;
}
if ((*root)->getVal()->getName() > data->getName()) {
placeNode(&(*root)->getLeft(), data); //Cannot take the address of an rvalue of type Node*
}
else {
placeNode(&(*root)->getRight(), data); //Cannot take the address of an rvalue of type Node*
}
return NULL;
}
【问题讨论】:
-
您的代码无法编译(由于与问题无关的问题)并且您没有说出错误是什么,因此很难提供帮助。 godbolt.org/g/RZFQXK
-
“右值错误”的描述性不是很好。
-
代码只是我的程序的简化版本。我已经编辑了上面的内容,现在准确地说明了错误。
标签: c++11 pointers object xcode7 binary-search-tree