[英]Address of pointer return value
I have the following .h file 我有以下.h文件
class Node
{
private :
int m_data;
Node* m_left;
Node* m_right;
public:
Node(int data) : m_data(data), m_left(nullptr), m_right(nullptr) {}
Node* getLeft() { return m_left; }
Node* getRight() { return m_right; }
int getData() { return m_data; }
};
class Tree
{
private :
Node * m_root;
unsigned int m_size;
void freeNode(Node*);
bool insert(Node**, int );
public:
Tree() : m_size(0) {}
~Tree();
int findMaximumVericalSum();
bool insert(int);
};
The implementation, I get an error - what is wrong and how should I fix it 实施时,出现错误-什么地方出了问题以及如何解决
'&' requires l-value
'&'需要l值
The address-of operator ( & ) must have an l-value as operand.
地址运算符(&)必须具有一个l值作为操作数。
bool Tree::insert(Node** root, int data)
{
Node* newNode = new Node(data);
if (*root == nullptr) {
*root = new Node(data);
}
else if (data < (*root)->getLeft()->getData())
{
// error '&' requires l-value
insert(&((*root)->getLeft()), data);
}
else if (data > (*root)->getLeft()->getData())
{
}
}
To my surprise, I wasn't able to find a good duplicate for the question as asked (other issues notwithstanding). 令我惊讶的是,我无法找到所问问题的一个很好的副本(尽管有其他问题)。
The error you see comes from the following piece of code: 您看到的错误来自以下代码:
&((*root)->getLeft())
And can be vastly simplified into following very little piece: 并可以大大简化为以下几小段:
int bar();
void foo(int*);
void baz() {
foo(&bar());
}
(also can be seen on https://gcc.godbolt.org/z/PuwtuT ) (也可以在https://gcc.godbolt.org/z/PuwtuT上看到)
This produces exactly the same compiler error. 这将产生完全相同的编译器错误。 And the reason for this is that C++ prohibits taking an address of prvalues, and while whole rvalue/lvalue discussion is super complicated, it is easy to remember that when function returns a value, it is always an prvalue.
这样做的原因是C ++禁止获取prvalue的地址,尽管整个rvalue / lvalue的讨论非常复杂,但很容易记住,当函数返回值时,它始终是prvalue。 (If function returns a reference, this is a different story).
(如果函数返回引用,则情况有所不同)。 You can, however, pass an rvalue by either const or rvalue reference.
但是,您可以通过const或rvalue引用传递右值。
I can not immediately offer a solution to your problem, because I am not super clear what is your end goal. 我无法立即为您的问题提供解决方案,因为我不清楚您的最终目标是什么。 But I do hope that this answer could at least steer your thoughts and allow you to come up with another question.
但我确实希望这个答案至少可以引导您的想法,并让您提出另一个问题。
Node* getLeft()
This function returns a prvalue. 此函数返回一个prvalue。
return m_left;
The prvalue is initialised from this expression. prvalue由该表达式初始化。 It is not possible to get address of
m_left
from the prvalue. 无法从prvalue获取
m_left
地址。 More generally, it is not possible to get the address of any prvalue. 更一般而言,不可能获得任何prvalue的地址。 The operand of the addressof operator must be an lvalue.
addressof运算符的操作数必须为左值。
&((*root)->getLeft()
what is wrong
怎么了
Here, the operand of the addressof operator is a prvalue. 在此,addressof运算符的操作数是prvalue。 The program is ill-formed.
该程序格式不正确。
how should I fix it
我应该如何解决
Don't try to apply the addressof operator to a prvalue expression. 不要尝试将addressof运算符应用于prvalue表达式。
If your intention is to pass a pointer to (*root)->m_left
into the function, then you need some way to access that member. 如果您打算将指向
(*root)->m_left
的指针传递给函数,则需要某种方式来访问该成员。
Furthermore, Tree::insert
is declared to return bool
, but lacks any return statements. 此外,
Tree::insert
被声明为返回bool
,但是缺少任何return语句。 Behaviour of the program is undefined (or would be if it was well-formed in the first place). 程序的行为是不确定的(或者如果它一开始就格式正确)。
PS Your function doesn't handle the case of data == (*root)->getLeft()->getData()
. PS您的函数无法处理
data == (*root)->getLeft()->getData()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.