Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the C++ error message "<near match>" mean?

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!)

like image 922
fuenfundachtzig Avatar asked Oct 07 '09 10:10

fuenfundachtzig


1 Answers

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:

  • Make the method const, if it doesn't modify the visible state of the object
  • Or pass around a non-const reference/pointer

If 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_casts 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.

like image 150
Douglas Leeder Avatar answered Sep 24 '22 10:09

Douglas Leeder