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
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.
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; }
passing const ... as this argument discards qualifiers [-fpermissive]
This
Complex operator - (Complex const &obj) { ...
and others are non-const member functions. Member functions are non-const by default, meaning they are declared to modify this
. You cannot call them on a const
instance. Most of your operators do not modify this
, hence should be declared as const
:
Complex operator - (Complex const &obj) const { ...
// ^^
error: passing 'const S' as 'this' argument discards qualifiers
Elements of a std::set<T>
are unmodifiable - set_of_S.begin()
returns a constant iterator: cppreference
Because both iterator and const_iterator are constant iterators (and may in fact be the same type), it is not possible to mutate the elements of the container through an iterator returned by any of these member functions [
begin
/cbegin
].
That means that the element pointed to by the iterator it
is const
, so you can't call a non-const function such as set_c
on it. it->c = 300
still works because you've made c
mutable. It has nothing to do with the lambda you're calling this in being mutable or not.
Passing ‘const ...’ as ‘this’ argument discards qualifiers
You got the iterator p
from an unordered_set
; the contents of unordered_set
are immutable (to avoid violating invariants), and the iterator returned is a const
view of the elements to ensure that.
You could mark children
as mutable
so it can be mutated even when the Concept_tree
is const
(since it's not part of equality/hash computation, this might be okay). But this will make it possible to mutate children
even when you want a Concept_tree
to be logically const. To avoid that, you could use an unordered_map
mapping just the id
(immutable key) to the actual Concept_tree
(mutable value) object.
Side-note: You probably want to mark your operator==
as const
so this
is const, or make it a non-member function with both argument const per What are the basic rules and idioms for operator overloading?.
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 Listint as this argument discards qualifiers within method definition
In this line:
List<int> findList = rollNo.findAll(rollNo[2]);
...you are calling the copy constructor of List<int>
that takes a const
-reference:
List(const List<T>& l)
In this constructor, you try to access the size of l
:
s = l.size();
...but you cannot because l
is const
-qualified and size()
is not, and you can only call const
-qualified method on const
-qualified variables.
To fix this, you need to const
-qualify size()
:
int size() const { return s; }
Here are some advice for your code, try to lookup a good recent C++ book for more:
- unless it is an exercice, do not make your own
List
, usestd::vector
; - use
nullptr
instead ofNULL
; - do not provide constructor or assignment operators that take non-constant lvalue-reference (
List(List<T>&)
andoperator=(List<T>&)
; - provide move-constructor and move-assignment operator (
List(List<T>&&)
andoperator=(List<T>&&)
), see the rule of 0/3/5; - return
List<T>&
from your assignment operators; - check the copy-and-swap idiom for your assignment operators;
const
-qualify the function that should be:size()
,find()
,lookup()
;- add
const
-qualified overload for the relevant method:const T& operator[](int i) const
; - check some C++ utility functions, such as
copy
orequal_range
.
Related Topics
What Is the Right Approach When Using Stl Container for Median Calculation
Exclude Source File in Compilation Using Makefile
How to Forward Declare a Template Class in Namespace Std
C++, How to Call a Constructor Directly, Without New
How to Temporarily Disable a MACro Expansion in C/C++
How to Speed Up Matrix Multiplication in C++
What's the C++ Suffix for Long Double Literals
When Pass-By-Pointer Is Preferred to Pass-By-Reference in C++
Why Use ++I Instead of I++ in Cases Where the Value Is Not Used Anywhere Else in the Statement
Qt4: Transparent Window with Rounded Corners
Performance Penalty for Working with Interfaces in C++
Returning Std::String/Std::List from Dll
How to Safely Average Two Unsigned Ints in C++
Name of Process for Active Window in Windows 8/10