一个奇怪的 C++ 错误:test.cpp:15:错误:传递“const *”; 作为“这个” “*”的论证 丢弃限定符
我在使用一段特定的代码时遇到了一些问题,如果有人能在这个问题上启发我,我将不胜感激,我已在以下示例中隔离了该问题:
#include <iostream>
using namespace std;
class testing{
int test();
int test1(const testing& test2);
};
int testing::test(){
return 1;
}
int testing::test1(const testing& test2){
test2.test();
return 1;
}
那么什么可能导致以下错误:
测试.cpp:15:错误:将“const测试”作为“int测试::test()”的“this”参数传递会丢弃限定符
非常感谢!
I'm having some trouble with a particular piece of code, if anyone can enlighten me on this matter it would be greatly appreciated, I've isolated the problem down in the following sample:
#include <iostream>
using namespace std;
class testing{
int test();
int test1(const testing& test2);
};
int testing::test(){
return 1;
}
int testing::test1(const testing& test2){
test2.test();
return 1;
}
So what could possibly have cause the following error:
test.cpp:15: error: passing ‘const testing’ as ‘this’ argument of ‘int testing::test()’ discards qualifiers
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题是从
const
对象test2
调用非const
函数test2.test()
>测试::test1。testing::test1
获取test2
作为参数const testing &test2
。 所以在testing::test1
中,test2const
。 然后在函数的第一行:在
test2
上调用testing::test
函数。 该函数没有在签名末尾使用 const 进行声明,因此它可能会修改调用它的对象(隐式传递给它的 this 指针),即使它不,编译器假设如此。 通过让您在那里调用它,编译器将允许您修改 const 变量而无需显式强制转换,而 C++ 不允许这样做。 因此解释一下错误消息:this
指的是成员函数(testing::test
)操作的对象,在本例中它不是const
,因为testing::test
没有用const
声明,因此在尝试创建非testing::test
时会检测到不匹配。 code>const 指针 (this
) 引用const
对象 (testing
),忽略const
> 限定符。要解决这个问题,请确定
testing::test
函数是否需要修改调用它的对象(现在的编写方式不需要,因为所有它确实是return 1
,但是这可能会改变,所以你需要考虑它的预期功能是什么)。 如果应该,那么显然在 const 对象上调用它是不好的,尽管您可以使用 const_cast 要求编译器覆盖它,但这很危险。 如果不应该,则将其标记为 const,以便也可以在 const 对象上调用它:The problem is calling a non-
const
functiontest2.test()
on aconst
objecttest2
fromtesting::test1
.testing::test1
getstest2
as a parameterconst testing &test2
. So withintesting::test1
,test2const
. Then in the first line of the function:The
testing::test
function is called ontest2
. That function is not declared withconst
at the signature end, so it may modify the object it is called on (thethis
pointer implicitly passed to it), and even though it does not, the compiler assumes so. By letting you call it there, the compiler would let you modify aconst
variable without an explicit cast, which C++ is not supposed to allow. Therefore to explain the error message:this
refers to the object the member function (testing::test
) operates on, and in this case it is notconst
, becausetesting::test
was not declared withconst
, and thus the mismatch is detected when trying to make a non-const
pointer (this
) refer to aconst
object (testing
), ignoring theconst
qualifier.To solve this, decide whether the
testing::test
function should ever need to modify the object it is called on (the way it is written now it does not, as all it does isreturn 1
, however that may change, so you need to think at what its intended functionality is). If it should, then obviously calling it on aconst
object is bad, although you can useconst_cast
to ask the compiler to override that, but this is dangerous. If it should not, then mark itconst
, so that it can be called onconst
objects as well:由于成员函数 test1 的定义:
您正在传递变量 test2 的 const 引用。
这意味着您不能修改 test2 的任何成员,也不能调用任何非 const 或非 static 的成员函数。
修复方法如下:
末尾的额外 const 告诉编译器您不打算修改当前对象的内容(如果这样做,您将得到不同的编译错误)。
Because of the definition of the member function test1:
You are passing in a const reference of for the variable test2.
That means that you cannot modify any member of test2 and you cannot call any member function that is not const or is not static.
Here is how you can fix:
The extra const at the end tells the compiler that you are not planning on modifying the content of the current object (and if you do you will get a different compiling error).
该行:
test2.test()
正在调用非 const 函数,即使 test2 是 const 引用。 那就是问题所在。 您可以通过将 test::test 设置为 const 函数来解决此问题。
The line:
test2.test()
is calling a non const function, even though test2 is a const reference. That's the problem. You can fix this by making testing::test a const function.
test::test1(const testing& test2) 期望传递的对象是 const,如果您修改其变量的值,或访问未显式定义为 const 的任何方法,它将给您一个错误。
由于 test() 方法实际上不会更改任何数据,因此最佳实践是将其设置为 const,如下所示:
或者,在定义 test1() 的参数时删除 const 一词,它将允许您访问以下任意数据:闲暇时传递对象的方法。
testing::test1(const testing& test2) is expecting the passed object to be const, and it will give you an error if you either modify the values of it's variables, or access any methods thereof that are not explicitly defined as const.
Since the test() method doesn't actually change any data, best practice is to set it const, as follows:
Alternatively, just remove the word const when defining the arguments for test1(), and it will allow you to access any of the passed object's methods at your leisure.
要获得快速而肮脏的解决方案,请尝试使用
-fpermissive
进行编译,正如编译器本身经常建议的那样(这可能是 VisualStudio 编译器所做的,因为 Windows 用户很少报告此问题)。For a quick and dirt solution, try compiling with
-fpermissive
as often suggested by the compiler itself (which is probably what VisualStudio compilers do, being that Windows users seldom report this problem).