When compiling my code with the GNU C++ compiler I get something like
bla.cxx: In function `int main(int, const char**)':
bla.cxx:110: error: no matching function for call to `func(const classA*&, const classB<classC>*&) const'
someheader.h:321: note: candidates are: bool func(const classA*, const T*&, const std::string&, std::string&) [with T = classB<classC>] <near match>
What does <near match>
indicate and how do I fix this error?
(I simplified the error message as much as possible without (hopefully) removing necessary information. Actually, I'd rather not put an explicit example here, but encourage general replies to the question!)
I normally see <near match>
when a possible method matches except for const
. Maybe the strings are optional arguments in this case? In that case the problem is that you have a const object, and are trying to call a non-const method.
NB: I haven't ever looked at the compiler code, merely looked at error messages gcc has generated.
EDIT:
From your comment, the strings at the end are optional, so aren't the problem. Assuming that is the method you want to call, then the problem is that you have a const pointer/reference to the object, and are trying to call a non-const method. To fix this, either:
const
, if it doesn't modify the visible state of the objectIf neither of those options is at all possible, and as a last resort and you can't change either of these things you can const_cast
the pointer/reference, but then you are leaving a very bad smell in the code.
We do have a few places that do const_cast
s in our code, but only when calls old C functions, that take a non-const pointer but don't modify it. In straight C++ code that you control you can avoid const_cast
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With