每次将邻居A添加到节点B时,我都要创建一个无向图。我也必须将节点B添加为A的邻居,但是我的方法行不通。Non-const lvalue reference to type 'Element *' cannot bind to a temporary of type 'Element *'
class Element
{
std::vector<Element *> m_neighbours;
private:
public:
void addNeighbour(Element*& neighbour)
{
m_neighbours.push_back(neighbour);
neighbour->addNeighbour(this);
}
};
最佳答案
为了理解问题所在,让我们假设您编写了以下代码:
void addNeighbour(Element*& neighbour)
{
m_neighbours.emplace_back(neighbour);
neighbour->addNeighbour(this);
neighbour = nullptr; // <--- This is new
}
现在,考虑一下进行此调用时会发生什么:
neighbour->addNeighbour(this);
对该函数的调用按引用传递
this
,意思是“请随时重新分配this
”。然后,在函数调用中,实际上,最后一行试图将neighbour
重新分配给nullptr
。但这是一个问题,因为你不会写this = nullptr; // Error!
因为
this
是一个右值。最简单的解决方法是不通过引用获取参数,因为在实际需要引用的地方您无需对其进行任何操作。只需输入一个
Element*
,说“请把您感兴趣的指针的副本交给我”。(独立地-您的代码会给您带来麻烦,因为调用
A->addNeighbour(B)
会调用B->addNeighbour(A)
,后者会调用A->addNeighbour(B)
,然后会调用B->addNeighbour(A)
,等等。直到您耗尽调用堆栈。您应该在此处添加一个检查以确保如果Element
已被记录,则无需再次添加。为此,您可能希望将m_neighbours
设为std::unordered_set
而不是std::vector
。)关于c++ - 非常量左值引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61066344/