• 12
• 9
• 9
• 13
• 10

# error: passing 'const X'...discards qualifiers (C++)

This topic is 3561 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I don't recall ever seeing this error, so I'm afraid I don't know what it's complaining about. Like anything, this issue lies in a few layers of pointers. I have a pointer to a vector of pointers to vectors. I want to add objects to the most deeply nested vector. (These are Qt vectors which are functionally identical - so far - to STL vectors.)
_imagestore->at(target_bin.get_id()).push_back(pix_list->front());

Explaining precisely what my goal is here could take pages, so I'm trying to gloss over it. It is the last expression that throws the error...pix_list is a Qt vector of images, and I want to push the top image onto my more complicated matrix of image-vectors. I get this error: "error: passing 'const ImageVector' as 'this' argument of 'void QVector<T>::push_back(const T&)[with T = Image*] discards qualifiers." I have *no* idea what it's talking about =( What qualifiers? I realize there may be more info necessary to answer this, so feel free to ask and I will supply info as best I can without spilling my rather large program into a window. I'm using C++ in X Code in OS X, and the only non-standard library I'm using is Qt (for my gui). EDIT: For the record, the word "const" does not appear in my project.

##### Share on other sites
Look at the signature of imagestore->at().

It's likely there are two versions, a const and non-const.

The way you're passing, incorrect version gets matched. As such, at() returns const Something, but push_back() is declared non-const.

The 'this' implies that the problem lies in declaration of the object you are calling the member function on (imagestore in this case).

Quote:
 Explaining precisely what my goal is here could take pages

This is a sure sign that whatever you're doing is horribly convoluted and messy. No class! should take more than one reasonably sized sentence to explain.

Quote:
 For the record, the word "const" does not appear in my project

Oh, but it does. In third-party libraries, as well as in implicit conversions and parameter passing. Gotta love C++.

##### Share on other sites
Quote:
 Original post by serratemplarI have *no* idea what it's talking about =( What qualifiers?

It is referring to cv-qualifiers. Thoes being the const and volatile keywords.

##### Share on other sites
How is _imagestore declared?

##### Share on other sites
As Antheus suggested, but after actually looking at the documentation, QVector::at exists only as const-member. Use QVector::operator [] instead for non-const return value to call QVector::push_back on.

##### Share on other sites
Uhg. Each time I hit reply to add something I find it's already been said. This cycle has occured for 3 replies in this thread so far. I'm quoting the last one for emphasis since it seems to be the problem and solution:

Quote:
 Original post by Brother BobAs Antheus suggested, but after actually looking at the documentation, QVector::at exists only as const-member. Use QVector::operator [] instead for non-const return value to call QVector::push_back on.

So one of:

_imagestore->operator[](target_bin.get_id()).push_back(pix_list->front());
(*_imagestore)[target_bin.get_id()].push_back(pix_list->front());

Should work, if const doesn't appear in your code as you say it doesn't. Qt seems to be a poor copy <_<

Also, to decode the error message:

"error: passing 'const ImageVector' as 'this' argument of 'void QVector<T>::push_back(const T&)[with T = Image*] discards qualifiers."

First, know that "const" is C++'s term for "immutable" or "unchangeable". "It always stays the same, it is a constant.":

const double pi = 3.14159265358979323;pi = 42.0; // ERROR, you shouldn't reassign this!

Basically, this means your code is doing something similar -- this would give the same error:

const ImageVector example;example.push_back(0); // ERROR, you're trying to modify a constant!

In this case, we can figure out that for this to happen, it must be the return type of at that's constant, since that's what you're calling push_back on. Hence the above investigation into at's documentation.

##### Share on other sites
I gotta say, it would have been much clearer if the error said: "Trying to call a non-const member function on a const object" or something similar.

##### Share on other sites
First, let me thank ALL of you for making this so much clearer. These boards have often come to my rescue, and often I say so ;) but it's true: you are all consistently my heroes.

MaulingMonkey: it was your modified statement that worked. I was trying this

_imagestore->[target_bin.get_id()].push_back(pix_list->front());

and that generated another error, "expected unqualified-id before '[' token".

In my mind, X-> is equivalent to (*X), so I never thought to swap them...why in this case was it not so? I tried various parens nests and got exactly that same error.

*** HERE FOLLOWS NON-QUESTION SPECIFIC BLATHER ***

Now, let me apologize to all of you for something that is very embarrasing to me. This code I am working on is a two-year teetering work in progress that I have put between two hours to ten hours a week into (always sporadically) as it was a pet-project for some unfunded research. I never had time to make it pretty as I was learning Qt (and relearning C++ after a long stint in math and theory), and well...hacks that should have been removed and made pretty again were left behind. All of a sudden there are people coming out of the woodwork who want to fund this, so my advisors are like GET IT DONE! GET IT DONE NOW!! So I have maybe three weeks to do it. It is not my proudest forray into programming. =( It's big. It's leaky. And I'm amazed it works as well as it does.

So, one reason I haven't posted code is that my advisors haven't assured me it's open source yet. Another reason is, frankly, I'm embarrassed by it. Even my overnight school projects were prettier and better formed than this rickety bit of trash. =( Even if it's ready to debut in two weeks, I have a LOT more work to do to make it pretty enough for open source.

##### Share on other sites
(*_imagestore)[target_bin.get_id()].push_back(pix_list->front());or(*_imagestore).operator[][target_bin.get_id()].push_back(pix_list->front());or_imagestore->operator[][target_bin.get_id()].push_back(pix_list->front());

Are equivalent.