错误:将"xxx"作为"xxx"的"this"参数传递会舍弃限定词[-fpermissive] [英] error: passing 'xxx' as 'this' argument of 'xxx' discards qualifiers [-fpermissive]

查看:69
本文介绍了错误:将"xxx"作为"xxx"的"this"参数传递会舍弃限定词[-fpermissive]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为c ++入门类编写程序,但收到一条我不理解的错误消息.我有一个称为小数的类,其成员函数为DividedBy.DividedBy的初始化和定义如下:

I'm writing a program for an introductory c++ class, and I'm getting an error message that I don't understand. I have a class called fraction, with a member function called DividedBy. DividedBy is initialized and defined as follows:

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:

I'm calling the function DividedBy like this:

result = f3.DividedBy(f4);

结果,f3和f4均为分数对象.我收到此错误消息:

result, f3, and f4 are all fraction objects. I'm getting this error message:

将常量分数"作为分数分数:: DividedBy(分数)"的此"参数传递时会丢弃限定词[-fpermissive]

passing 'const fraction' as 'this' argument of 'fraction fraction::DividedBy(fraction)' discards qualifiers [-fpermissive]

这是什么意思?我查看了此消息,似乎总是有人试图传递一个常量参数,我敢肯定我不在这儿做.我不知何故放弃了哪些预选赛?

What does this mean? I've looked up this message and it seems to always involve someone trying to pass a constant argument, which I'm pretty sure I'm not doing here. What are the qualifiers that I'm somehow discarding?

推荐答案

这意味着您已将f3声明为 const fraction 类型,这意味着您无法对其进行更改.即使方法 DividedBy 不会更改它,也无法保证,因此编译器会假定该方法可以以某种方式对其进行更改.

It means you have declared f3 as type const fraction, meaning you cannot change it. Even though the method DividedBy does not change it, there is no guarantee about it so compiler assumes the method can change it somehow.

这里的解决方案是不使f3恒定,或者通过将声明和定义更改为

The solution here is either not to have f3 constant or better make the method DividedBy constant by changing the declaration and definition to

fraction fraction::DividedBy(fraction operand) const   

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

根据经验法则,最好使任何可以恒定的东西都恒定.例如,您可以将操作数设置为 const fraction& 类型,这将防止不必要地复制您的类,同时在传递常量分数时仍不阻止代码编译.

By the rule of thumb it is better to make constant anything that can be constant. You can for example make the operand of type const fraction & which would prevent unnecessary copying of your classes around while still not preventing the code from compiling when you pass it constant fractions.

最后一部分 [-fpermissive] 只是编译器,它告诉您如何抑制此警告并使它无论如何都可以编译(您只需将此标志传递给编译器即可).很高兴知道有此选项,但是除非您真的确定自己在做什么以及为什么不能以正确的方式进行操作,否则切勿使用它.

The last part [-fpermissive] is just the compiler telling you how to suppress this warning and make it compile anyways (you would just pass this flag to the compiler). It's nice to know there is this option but you should never use it unless you are really sure what you're doing and why it can't be done the proper way.

这篇关于错误:将"xxx"作为"xxx"的"this"参数传递会舍弃限定词[-fpermissive]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆