error: passing xxx as 'this' argument of xxx discards qualifiers
The objects in the std::set
are stored as const StudentT
. So when you try to call getId()
with the const
object the compiler detects a problem, mainly you're calling a non-const member function on const object which is not allowed because non-const member functions make NO PROMISE not to modify the object; so the compiler is going to make a safe assumption that getId()
might attempt to modify the object but at the same time, it also notices that the object is const; so any attempt to modify the const object should be an error. Hence compiler generates an error message.
The solution is simple: make the functions const as:
int getId() const {
return id;
}
string getName() const {
return name;
}
This is necessary because now you can call getId()
and getName()
on const objects as:
void f(const StudentT & s)
{
cout << s.getId(); //now okay, but error with your versions
cout << s.getName(); //now okay, but error with your versions
}
As a sidenote, you should implement operator<
as :
inline bool operator< (const StudentT & s1, const StudentT & s2)
{
return s1.getId() < s2.getId();
}
Note parameters are now const
reference.
error: passing 'const …' as 'this' argument of '…' discards qualifiers
Your hi
method is not declared as const
inside your A class. Hence, the compiler cannot guarantee that calling a.hi()
will not change your constant reference to a
, thus it raises an error.
You can read more about constant member functions here and correct usage of the const
keyword here.
error: passing 'xxx' as 'this' argument of 'xxx' discards qualifiers [-fpermissive]
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.
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 {
...
}
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.
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.
error: passing 'xxx' as 'this' argument discards qualifiers
You're calling a non-const method print()
on a const object. A const
method is such that cannot modify the object it's called on, and this is the only sort of member methods you are allowed to call on const
objects (to preserve the const-ness).
A const method is denoted by const
after the method arguments list:
void print() const {cout << a << b << c;}
And yes, const void
is useless at best, just void
is all the same.
Error: passing const xxx as this argument of xxx discards qualifiers
Two things:
Define your
bool operator()
asconst
. It's just a good practice. This tells the compiler that this function will not have side-effects on the member variables of the class.Add
const &
qualifiers to thelhs
andrhs
arguments. Passing constant references instead of copying memory all over the place is also good practice. By declaring references asconst
you're telling compiler that this function should not have side-effects on the referenced objects.
Your operator()
should look as follows:
bool operator() (const string &lhs, const string &rhs) const
{
return strcasecmp(lhs.c_str(), rhs.c_str()) < 0;
}
Error when compiling: passing (something) as 'this' argument discards qualifiers
The first problem is that
const string type;
is a const
member. That means, after it's initialization you can't assign any new value to it.
The second problem is, that the getter is also marked const
:
// VVVVV
const string & getType() const override
Meaning you can't modiy member data from inside this function. Not to mention that it's a bad design if a getter changes a value before returning it.
If this override of getType
is suppsed to always return "Greedy"
you could do something like this:
const string & getType() const override {
static std::string greedyType = "Greedy";
return greedyType;
}
Error: passing 'const xxx' as 'this' argument discards qualifiers
Ok so after googling a bit more, i realized that this pointer is implicitly passed to the oveloaded * and then on to karatsuba (as it is a member function of the class), and as karatsuba is not a constant function, there is no guarantee that it won't change the object contents, hence this error is triggered.
One solution is to declare karatsuba as static, as static member functions don't receive this pointer (they can even be called with out a class object simply using :: operator) , read more about them from here Static data members and member functions.
All that is needed to be changed is :-
static vi karatsuba(vi a,vi b)
{
int n = a.size();
if(n <= 16)
{
// some code
}
// some code
return a;
}
compiler error: passing ‘const something’ as ‘this’ argument discards qualifiers
You have to make length
to be const-qualified:
int length(){ return m_count; }
→
int length() const { return m_count; }
Error: passing x as 'this' argument of x discards qualifiers
You want to allow your compare operators to be called on a const
object, so replace
bool operator < (const Heart &o) {
with
bool operator < (const Heart &o) const {
and so on.
Also note that you have implemented operator = ()
rather than operator == ()
Related Topics
Std::Endl Is of Unknown Type When Overloading Operator≪≪
Comparing a Variable to a Range of Values
What Exactly Is the "Immediate Context" Mentioned in the C++11 Standard For Which Sfinae Applies
Why Are Function Pointers and Data Pointers Incompatible in C/C++
Move Assignment Operator and 'If (This != &Rhs)'
Check If a String Contains a String in C++
Calculate the Factorial of an Arbitrarily Large Number, Showing All the Digits
Virtual Functions and Performance - C++
Is Std::Vector or Boost::Vector Thread Safe
When Can Outer Braces Be Omitted in an Initializer List
What Is the Windows Equivalent For En_Us.Utf-8 Locale
Replace Substring With Another Substring C++
How to Combine Hash Values in C++0X
Why Do We Need a Pure Virtual Destructor in C++
When Vectors Are Allocated, Do They Use Memory on the Heap or the Stack
Are "Anonymous Structs" Standard? And, Really, What *Are* They