简体   繁体   English

'std :: list <_Ty> :: push_back':无法将'this'指针从'const std :: list <_Ty>'转换为'std :: list <_Ty>&'

[英]'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.

相关问题 无法将&#39;this&#39;指针从&#39;std :: stack &lt;_Ty&gt;&#39;转换为&#39;std :: stack &lt;_Ty&gt;&&#39; - Cannot convert 'this' pointer from 'std::stack<_Ty>' to 'std::stack<_Ty> &' 错误C2440:“ =”:无法从“ std :: list”转换 <std::string,std::allocator<_Ty> &gt; *&#39;到&#39;std :: string *&#39; - error C2440: '=': cannot convert from 'std::list<std::string,std::allocator<_Ty>> *'to 'std::string*' 无法从&#39;std :: vector &lt;_Ty&gt;&#39;转换为&#39;std :: vector &lt;_Ty&gt;& - cannot convert from 'std::vector<_Ty>' to 'std::vector<_Ty> & 错误C2440:&#39;初始化&#39;:无法从&#39;初始化列表&#39;转换为&#39;std :: vector <char *,std::allocator<_Ty> &gt;” - error C2440: 'initializing': cannot convert from 'initializer list' to 'std::vector<char *,std::allocator<_Ty>>' 错误C2664:&#39;void std :: vector &lt;_Ty&gt; :: push_back(_Ty &amp;&amp;)&#39;:无法从&#39;Node转换参数1 <T> *&#39;到&#39;节点 <T> &amp;&amp;” - error C2664 : 'void std::vector<_Ty>::push_back(_Ty&&)': cannot convert parameter 1 from 'Node<T> *' to 'Node<T>&&' std :: pair &lt;_Ty1,_Ty2&gt; :: pair &lt;_Ty1,_Ty2&gt;&无法转换参数 - std::pair<_Ty1,_Ty2>::pair<_Ty1,_Ty2>& Cannot convert parameter 无法从std :: shared_ptr &lt;_Ty&gt;转换为std :: shared_ptr &lt;_Ty&gt; - Cannot convert from std::shared_ptr<_Ty> to std::shared_ptr<_Ty> 错误C2782:&#39;const _Ty&std :: min(const _Ty&,const _Ty&)&#39;:模板参数&#39;_Ty&#39;不明确 - error C2782: 'const _Ty &std::min(const _Ty &,const _Ty &)' : template parameter '_Ty' is ambiguous “MessageBoxA”:无法将参数 2 从“std::vector<_Ty>”转换为“LPCSTR” - 'MessageBoxA' : cannot convert parameter 2 from 'std::vector<_Ty>' to 'LPCSTR' std :: allocator &lt;_Ty&gt; :: deallocate:无法将参数1从&#39;X&#39;转换为&#39;Y&#39; - std::allocator<_Ty>::deallocate : cannot convert parameter 1 from 'X' to 'Y'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM