C++ “对类型的非常量左值引用无法绑定”错误与引用(类型 &)但不是指针(类型 *)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14754355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:39:42  来源:igfitidea点击:

"non-const lvalue reference to type cannot bind" error with reference (Type &) but not with pointer (Type *)

c++pointersreferenceconstlvalue

提问by Alexis Cofion

I am getting this error "Non-const lvalue to type 'Cell' cannot bind to a temporary of type 'Cell *' with this code :

我收到此错误“输入'Cell'的非const左值无法使用以下代码绑定到'Cell *'类型的临时文件:

class RegionHolder
{    
public:
    RegionHolder(Region& Region1):m_RegionCellNOO(&(Region1.m_NOO))
    ~RegionHolder();

protected:
    Cell & m_RegionCellNOO; // difference is here   
};

but not with this one :

但不是这个:

class RegionHolder
{    
public:
    RegionHolder(Region& Region1):m_RegionCellNOO(&(Region1.m_NOO))
    ~RegionHolder();

protected:
    Cell * m_RegionCellNOO; // difference is here   
};

I don't understand the problem and would really like to use references and not pointers.

我不明白这个问题,真的很想使用引用而不是指针。

Thanks

谢谢

回答by Mike Seymour

You forgot to show us the definition, but presumably Region1.m_NOOis an object of type Cell. Your first example is taking the address of it, and trying to use the resulting pointer to initialise a reference. References aren't initialised from pointers, but from the object itself:

你忘了向我们展示定义,但大概Region1.m_NOO是一个类型的对象Cell。您的第一个示例是获取它的地址,并尝试使用结果指针来初始化引用。引用不是从指针初始化,而是从对象本身初始化:

RegionHolder(Region& Region1):m_RegionCellNOO(Region1.m_NOO) {}
//                                            ^ no &         ^^ don't forget that

There is one caveat with using references rather than pointers: they are not assignable, and so neither is your class. Often that's not a problem; but if you do need your class to be assignable, then you'll need to use a pointer instead.

使用引用而不是指针有一个警告:它们是不可赋值的,所以你的类也不是。通常这不是问题;但是如果你确实需要你的类是可分配的,那么你需要使用一个指针来代替。

回答by Hyman Aidley

The unary &operator gets a pointer to a variable. So, in C++ (as in C) a = &bgets a pointer to band stores this value in a, so if bis of type int, aneeds to be of type int*. Assignment to references, on the other hand, is implicit, so if a is of type int&you simply need to write a=bto make aa reference to b. So in your case, you need to rewrite your constructor as

一元运算&符获取指向变量的指针。因此,在 C++ 中(如在 C 中)a = &b获取指向b并将此值存储在 中的指针a,因此如果b是 type int,则a需要是 type int*。另一方面,对引用的赋值是隐式的,因此如果 a 是类型,int&您只需要编写a=ba引用b. 因此,在您的情况下,您需要将构造函数重写为

RegionHolder(Region& Region1):m_RegionCellNOO(Region1.m_NOO) {}

However, I think you're better off using pointers than references here anywayand trying to use C++ without getting comfortable with pointers is a very bad idea. So I suggest you take the time to make yourself comfortable with using pointers instead of trying to avoid them.

但是,我认为无论如何在这里使用指针比使用引用更好并且尝试使用 C++ 而不熟悉指针是一个非常糟糕的主意。所以我建议你花点时间让自己适应使用指针而不是试图避免它们。