Discards Qualifiers Error

Discards qualifiers error

Your checkElements() function is not marked as const so you can't call it on const qualified objects.

top(), however is const qualified so in top(), this is a pointer to a const Stack (even if the Stack instance on which top() was called happens to be non-const), so you can't call checkElements() which always requires a non-const instance.

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.

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

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.

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 obj' as 'this' argument discards qualifiers

You should make it a const member function explicitly:

    void show() const { cout << "addr = " << this << ", i_ = " << i_ << endl; }


Related Topics



Leave a reply



Submit