奇怪的C ++错误:test.cpp:15:error:传递'const *'作为'*'的'this'参数丢弃限定符 [英] An odd C++ error: test.cpp:15: error: passing ‘const *’ as ‘this’ argument of ‘*’ discards qualifiers
问题描述
我对某一段代码有一些麻烦,如果任何人可以启发我的这个问题,这将是非常感谢,我已经隔离了下面的示例中的问题:
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& testObj);
};
int testing::test(){
return 1;
}
int testing::test1(const testing& testObj){
testObj.test();
return 1;
}
因此,可能会导致以下错误:
So what could possibly have cause the following error:
test.cpp:15:error:传递'const testing'作为'int testing :: test()'的'this'参数丢弃限定符
test.cpp:15: error: passing ‘const testing’ as ‘this’ argument of ‘int testing::test()’ discards qualifiers
非常感谢!
推荐答案
问题是调用一个非 - const
函数 test2.test()
在 const
对象 test2
从 testing :: test1
。
The problem is calling a non-const
function test2.test()
on a const
object test2
from testing::test1
.
testing :: test1
获取
test2
作为参数 const testing& test2
。因此,在 testing :: test1
, test2const
中。然后在函数的第一行:
testing::test1
gets test2
as a parameter const testing &test2
. So within testing::test1
, test2const
. Then in the first line of the function:
test2.test()
testing :: test
函数在 test2
。该函数不是在签名结尾用 const
声明的,所以它可以修改它所调用的对象( this
指针隐式传递给它),即使它没有,编译器假设这样。通过让你调用它,编译器会让你修改一个 const
变量没有显式转换,C ++不允许。 因此要解释错误消息:
The testing::test
function is called on test2
. That function is not declared with const
at the signature end, so it may modify the object it is called on (the this
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 a const
variable without an explicit cast, which C++ is not supposed to allow. Therefore to explain the error message:
test.cpp:15: error: passing ‘const testing’ as ‘this’ argument of ‘int testing::test()’ discards qualifiers
this
指的是成员函数( testing :: test
)操作的对象,在这种情况下,它不是 const
,因为 testing :: test
未使用 const
因此当尝试使非< - code> const 指针( this
)引用<$ c $时检测到不匹配忽略 const
限定符
this
refers to the object the member function (testing::test
) operates on, and in this case it is not const
, because testing::test
was not declared with const
, and thus the mismatch is detected when trying to make a non-const
pointer (this
) refer to a const
object (testing
), ignoring the const
qualifier.
要解决此问题,请决定 testing :: test
函数应该需要修改它被调用的对象(它现在写的方式不是,因为它是 return 1
,但是可能会改变,所以你需要考虑它的预期功能是什么)。如果它应该,然后明显地调用它在一个 const
对象是坏的,虽然你可以使用 const_cast
要求编译器以覆盖它,但这是危险的。如果不应该,则标记它 const
,以便它可以在 const
对象上调用:
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 is return 1
, however that may change, so you need to think at what its intended functionality is). If it should, then obviously calling it on a const
object is bad, although you can use const_cast
to ask the compiler to override that, but this is dangerous. If it should not, then mark it const
, so that it can be called on const
objects as well:
class testing{
int test1() const;
// ...
}
int testing::test() const {
// ...
}
这篇关于奇怪的C ++错误:test.cpp:15:error:传递'const *'作为'*'的'this'参数丢弃限定符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!