错误:将“xxx”作为“xxx”的“this”参数传递会丢弃限定符 [-fpermissive]

克罗尼西亚德

我正在为一个介绍性的 c++ 类编写程序,但收到一条我不明白的错误消息。我有一个名为fraction 的类,有一个名为DividedBy 的成员函数。DividedBy 的初始化和定义如下:

fraction fraction::DividedBy(fraction operand)      

fraction fraction::DividedBy(fraction operand){
int quotNum = num * operand.num;
int quotDenom = denom * operand.denom;

simplify(quotNum, quotDenom);

fraction quot = fraction(quotNum, quotDenom);
return quot;}

我像这样调用函数 DividedBy:

result = f3.DividedBy(f4);

result、f3 和 f4 都是分数对象。我收到此错误消息:

将 'const fraction' 作为 'fraction fraction::DividedBy(fraction)' 的 'this' 参数传递会丢弃限定符 [-fpermissive]

这是什么意思?我查了这条消息,似乎总是有人试图传递一个常量参数,我很确定我不是在这里做的。我以某种方式丢弃了哪些限定符?

奥尔多森

这意味着您已将 f3 声明为 type const fraction,这意味着您无法更改它。即使该方法DividedBy没有改变它,也不能保证它,所以编译器假设该方法可以以某种方式改变它。

这里的解决方案要么不使用 f3 常量,要么DividedBy通过将声明和定义更改为更好地使方法保持不变

fraction fraction::DividedBy(fraction operand) const   

fraction fraction::DividedBy(fraction operand) const {
...
}

根据经验,最好使任何可以保持不变的事物保持不变。例如,您可以创建类型的操作数const fraction &,以防止不必要的类复制,同时在传递常量分数时仍然不会阻止代码编译。

最后一部分[-fpermissive]只是编译器告诉您如何抑制此警告并使其编译(您只需将此标志传递给编译器)。很高兴知道有这个选项,但除非您真的确定自己在做什么以及为什么不能以正确的方式完成,否则永远不要使用它。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

错误:将“ const std :: map <int,int>”作为“ this”参数传递会丢弃限定符[-fpermissive]

C ++错误:将“ const std :: vector <Node>”作为“ this”参数传递会丢弃限定符[-fpermissive]

错误:将“const Flacon”作为“this”参数传递会丢弃限定符 [-fpermissive]

错误:将 'const Player' 作为 'this' 参数传递会丢弃限定符 [-fpermissive]|

错误:传递'constas'this'参数会丢弃限定符[-fpermissive]

将 'const QVariant' 作为 'this' 参数传递会丢弃限定符 [-fpermissive]

错误:将'const Vector <int>'作为...的'this'参数传递时,将丢弃限定符[-fpermissive]

将'const CMyclass'作为...的'this'参数传递时,将丢弃限定符[-fpermissive]

使用map :: erase()时出现“错误:将'const ***'传递为'***'的'this'参数会丢弃限定符[-fpermissive]”

错误:将xxx作为xxx的“ this”参数传递会丢弃限定符

编译器“错误:将'const something'作为'this'参数传递会丢弃限定符”

错误:将“ const…”作为“…”的“ this”参数传递会丢弃限定符

错误:将“const S”作为“this”参数传递会丢弃限定符

C ++ Boost-序列化错误-将'const B'作为'this'参数传递会丢弃限定符

tbb::parallel_for 将 'const value_type' 作为 'this' 参数传递会丢弃限定符

错误:将x传递为x的'this'参数会丢弃限定符

错误:将“ const…”作为“…”的“ this”参数传递时会丢弃限定符

错误:从'int'到'void *'的无效转换[-fpermissive]

错误:从'void *'到'arguments *'的无效转换[-fpermissive]

方法定义中的错误“传递 const List<int> 作为此参数丢弃限定符”

错误:将“ const xxx”作为“ this”参数传递会舍弃限定词

在C ++中实现回调时出现c ++ -fpermissive错误

C++ 链表错误:获取右值地址 [-fpermissive]

错误:跳转到标签“失败” [-fpermissive],GCC与VS

(C++) 错误:从“char”到“const char*”的无效转换 [-fpermissive]

错误:接收临时[-fpermissive]的地址-npc_multivendor

C-[错误]从'char'到'const char *'的无效转换[-fpermissive]

错误:从“char”到“const char*”的无效转换[-fpermissive]|

“从 'const char*' 到 'char' [-fpermissive] 错误的无效转换”不会消失。