一个奇怪的 C++ 错误:test.cpp:15:错误:传递“const *”; 作为“这个” “*”的论证 丢弃限定符

发布于 2024-07-13 12:06:16 字数 430 浏览 9 评论 0原文

我在使用一段特定的代码时遇到了一些问题,如果有人能在这个问题上启发我,我将不胜感激,我已在以下示例中隔离了该问题:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

静若繁花 2024-07-20 12:06:16

问题是从 const 对象 test2 调用非 const 函数 test2.test() >测试::test1。

testing::test1 获取 test2 作为参数 const testing &test2。 所以在 testing::test1 中,test2const。 然后在函数的第一行:

test2.test()

test2 上调用 testing::test 函数。 该函数没有在签名末尾使用 const 进行声明,因此它可能会修改调用它的对象(隐式传递给它的 this 指针),即使它不,编译器假设如此。 通过让您在那里调用它,编译器将允许您修改 const 变量而无需显式强制转换,而 C++ 不允许这样做。 因此解释一下错误消息

test.cpp:15: error: passing ‘const testing’ as ‘this’ argument of ‘int testing::test()’ discards qualifiers

this指的是成员函数(testing::test)操作的对象,在本例中它不是 const,因为 testing::test 没有用 const 声明,因此在尝试创建非 testing::test 时会检测到不匹配。 code>const 指针 (this) 引用 const 对象 (testing),忽略 const > 限定符

要解决这个问题,请确定 testing::test 函数是否需要修改调用它的对象(现在的编写方式不需要,因为所有它确实是return 1,但是这可能会改变,所以你需要考虑它的预期功能是什么)。 如果应该,那么显然在 const 对象上调用它是不好的,尽管您可以使用 const_cast 要求编译器覆盖它,但这很危险。 如果不应该,则将其标记为 const,以便也可以在 const 对象上调用它:

class testing{
    int test1() const;
    // ...
}

int testing::test() const {
    // ...
}

The problem is calling a non-const function test2.test() on a const object test2 from testing::test1.

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()

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 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.

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 {
    // ...
}
剩一世无双 2024-07-20 12:06:16

由于成员函数 test1 的定义:

int testing::test1(const testing& test2){
   test2.test();
   return 1;
}

您正在传递变量 test2 的 const 引用。

这意味着您不能修改 test2 的任何成员,也不能调用任何非 const 或非 static 的成员函数。

修复方法如下:

int testing::test() const {
   return 1;
}

末尾的额外 const 告诉编译器您不打算修改当前对象的内容(如果这样做,您将得到不同的编译错误)。

Because of the definition of the member function test1:

int testing::test1(const testing& test2){
   test2.test();
   return 1;
}

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:

int testing::test() const {
   return 1;
}

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).

宛菡 2024-07-20 12:06:16

该行:
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.

知足的幸福 2024-07-20 12:06:16

test::test1(const testing& test2) 期望传递的对象是 const,如果您修改其变量的值,或访问未显式定义为 const 的任何方法,它将给您一个错误。

由于 test() 方法实际上不会更改任何数据,因此最佳实践是将其设置为 const,如下所示:

class testing{
   int test() const;
   int test1(const testing& test2);
};

int testing::test() const {
   return 1;
}

或者,在定义 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:

class testing{
   int test() const;
   int test1(const testing& test2);
};

int testing::test() const {
   return 1;
}

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.

无边思念无边月 2024-07-20 12:06:16

要获得快速而肮脏的解决方案,请尝试使用 -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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文