[英]'std::list<_Ty>::push_back' : cannot convert 'this' pointer from 'const std::list<_Ty>' to 'std::list<_Ty> &'
I'm getting this error message when try to compile the following 我在尝试编译以下内容时收到此错误消息
void MyClass::MyFunc() const
{
int i= 0;
myList.push_back(i);
}
I've already had to add the const to the function, despite it returning void
, to fix an identical problem with the invocation of the function itself, but now that it's occurring with a member of the STL, I'm thinking something is very wrong with my code. 我已经不得不将const添加到函数中,尽管它返回void
,修复了函数本身调用的相同问题,但是现在它与STL的成员一起发生,我在想是非常的我的代码错了。 For reference, the error was 作为参考,错误是
cannot convert 'this' pointer from 'const MyClass' to 'MyClass &'
I actually have very little idea of what's going on since I inherited this from someone with a very abusive relationship with coding standards and best practices. 事实上,我很少知道自从我从与编码标准和最佳实践有非常滥用关系的人那里继承了这些内容后发生了什么。 If someone could tell me what problem this is a symptom of and what I should be looking for in the code, it would help me a lot. 如果有人可以告诉我这是代码的症状和我应该寻找什么问题,它会对我有很大的帮助。
如果myList
是的数据成员MyClass
,具有push_back()
要修改的myList
的数据成员(因此要修改的实例MyClass
指向通过this
),因此MyFunc()
方法不能被标记为const
。
const
on a method means that it can't modify the instance it is called on, including any instance members. 方法上的const
意味着它不能修改它被调用的实例,包括任何实例成员。
You need to take a look at the code base and distinguish between methods that can modify the class instance (which should not be const
) and those that just provide information about the class instance (which should be const
). 您需要查看代码库并区分可以修改类实例(不应该是const
)的方法和仅提供有关类实例(应该是const
)的信息的方法。
$9.3.1/3: When an id-expression (5.1) that is not part of a class member access syntax (5.2.5) and not used to form a pointer to member (5.3.1) is used in a member of class X in a context where this can be used (5.1.1), if name lookup (3.4) resolves the name in the id-expression to a non-static non-type member of some class C, and if either the id-expression is potentially evaluated or C is X or a base class of X, the id-expression is transformed into a class member access expression (5.2.5) using (*this) (9.3.2) as the postfix-expression to the left of the . $ 9.3.1 / 3:当一个id-expression(5.1)不属于类成员访问语法(5.2.5)并且不用于形成指向成员(5.3.1)的指针时,在类成员中使用在可以使用它的上下文中的X(5.1.1),如果名称查找(3.4)将id-expression中的名称解析为某个类C的非静态非类型成员,并且如果是id-expression可能被评估或C是X或X的基类,id-expression被转换为类成员访问表达式(5.2.5)使用(* this)(9.3.2)作为左侧的后缀表达式这个。 operator. 运营商。
What this means is that the expression 这意味着表达方式
myList.push_back(i);
is treated as 被视为
(*this).myList.push_back(i);
$9.3.1/4: A non-static member function may be declared const, volatile, or const volatile. $ 9.3.1 / 4:非静态成员函数可以声明为const,volatile或const volatile。 These cv-qualifiers affect the type of the this pointer (9.3.2). 这些cv限定符会影响this指针的类型(9.3.2)。
$9.3.2/1: In the body of a non-static (9.3) member function, the keyword this is a prvalue expression whose value is the address of the object for which the function is called. $ 9.3.2 / 1:在非静态(9.3)成员函数的主体中,关键字this是一个prvalue表达式,其值是调用该函数的对象的地址。 The type of this in a member function of a class X is X*. 类X的成员函数中的类型是X *。 If the member function is declared const, the type of this is const X*, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const volatile X*. 如果成员函数声明为const,则其类型为const X *,如果成员函数声明为volatile,则其类型为volatile X *,如果成员函数声明为const volatile,则此类型为const挥发性X *。
In summary, what this means is that type of 'this' in your member function which is 'const' is 'MyClass const *'. 总之,这意味着你的成员函数中的'this'类型是'const'是'MyClass const *'。 Effectively, it means that Myclass::MyFunc
is not allowed to modify the state of the object pointed to by the 'this' pointer. 实际上,这意味着不允许Myclass::MyFunc
修改'this'指针所指向的对象的状态。
Assuming myList is of type std::list, the method std::list::push_back modifies the instance variable 'myList' as it inserts the value of 'i' in the list. 假设myList的类型为std :: list,则std :: list :: push_back方法会修改实例变量'myList',因为它会在列表中插入'i'的值。
But this breaks the semantics of the 'const' member function MyClass::MyFunc
which promises that it won't modify the state of the object. 但这打破了'const'成员函数MyClass::MyFunc
的语义,它承诺不会修改对象的状态。
So, you really need to remove the 'const' from this member function or use const_cast to remove the constness for this specific operation. 因此,您确实需要从此成员函数中删除“const”或使用const_cast删除此特定操作的常量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.