C++:error: passing ‘const string’ as ‘this’ argument discards qualifiers [-fpermissive]

string.cpp: In function ‘int const_f(const string&)’:
string.cpp:6:4: error: passing ‘const string’ {aka ‘const std::__cxx11::basic_string’} as ‘this’ argument discards qualifiers [-fpermissive]
a=“abc”;
^~~~~
const string a= “abc”;
a=“abc”;

这个错误的解释是:传递一个‘const string’ 作为this参数,将导致舍弃限定符。也就是 const string里只有一个限定词 const,就会导致舍弃const。为什么会提示这个错误呢?
这个错误比较难理解的是这个 'this’参数;错误里为什么会专门提示这个this参数;这个就需要往后看,最后这一行会被翻译成一个赋值构造函数,而类里对于成员函数,就是通过这个隐式的参数作为修改对象的一个管道,this是一个隐藏的参数。而这个赋值构造的参数是一个非const this参数,所以才会出现这个错误。

In file included from /usr/include/c++/8/string:52,
from /usr/include/c++/8/bits/locale_classes.h:40,
from /usr/include/c++/8/bits/ios_base.h:41,
from /usr/include/c++/8/ios:42,
from /usr/include/c++/8/ostream:38,
from /usr/include/c++/8/iostream:39,
from string.cpp:1:
/usr/include/c++/8/bits/basic_string.h:707:7: note: in call to ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char; _Traits = std::char_traits; _Alloc = std::allocator]’
operator=(const _CharT* __s)
^~~~~~~~

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mzhan017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值