Odd error C ++: test.cpp: 15: error: passing 'const * as' of this argument' * discards qualifiers

I am having problems with a specific code, if someone can enlighten me on this issue, it would be very useful, I highlighted the problem in the following example:

#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 lead to the following error:

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

Thank you so much!

+41
c ++ const compiler-errors
Feb 15 '09 at 6:43
source share
5 answers

The problem is calling the const function not const for the const test2 from testing::test1 .

testing::test1 receives test2 as the const testing &test2 . So, inside testing::test1 , test2const . Then in the first line of the function:

 test2.test() 

The testing::test function is called on test2 . This function is not declared with const at the end of the signature, so it can change the object to which it is called (the this pointer, implicitly passed to it), and even if it is not, the compiler assumes this. Letting you name it there, the compiler will let you change the const variable without an explicit cast, which C ++ does not 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 on which the member function acts ( testing::test ), and in this case it is not const , because testing::test not declared using const , and thus a mismatch was detected when trying to make a pointer not const ( this ) refers to a const ( testing ) object, ignoring the const qualifier.

To solve this problem , decide whether the testing::test function is needed to change the object it is called on (the way it is now written is not so, since all it does is return 1 , however this can change, so you need to think about what its intended functionality is). If so, then it is obvious that calling it on a const object is bad, although you can use const_cast to ask the compiler to override this, but it is dangerous. If this is not the case, then mark it with const so that it can also be called in const objects:

 class testing{ int test1() const; // ... } int testing::test() const { // ... } 
+64
Feb 15 '09 at 6:45
source share

Due to the definition of the member function test1:

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

You are passing a const reference to the variable test2.

This means that you cannot change any member of test2, and you cannot call any member function that is not constant or is not static.

Here is how you can fix it:

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

An additional constant at the end tells the compiler that you do not plan to change the contents of the current object (and if you do, you will get another compilation error).

+5
Feb 15 '09 at 7:02
source share

Line: test2.test ()

calls the non const function, although test2 is a constant reference. This is problem. You can fix this by testing :: test const.

+2
Feb 15 '09 at 6:46
source share

testing :: test1 (const testing & test2) expects the passed object to be const, and it will give you an error if you either change the values โ€‹โ€‹of these variables or gain access to any of their methods that are not explicitly defined as Set.

Since the test () method does not actually modify any data, it is best to set it const as follows:

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

Alternatively, simply remove the word const when defining the arguments for test1 (), and this will allow you to access any of the methods of the transferred objects in your free time.

+1
Feb 15 '09 at 7:11
source share

For a quick and dirty solution, try compiling with -fpermissive , as the compiler often suggests (this is probably what VisualStudio compilers do, as Windows users rarely report this problem).

-one
26 '15 at 14:18
source share



All Articles