views:

293

answers:

3

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

A: 

It means, most likely, that there's a mismatch between types at the calling site and at the function declaration. In particular, it means you've probably got one "const" too many or too few on one side or the other. The error message gave you the line numbers of the mismatched declaration/call.

Jonathan Feinberg
+4  A: 

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.

Douglas Leeder
I also tried adding two strings, but it didn't help. The two last arguments (strings) are optional.
fuenfundachtzig
Then you have a const object and are trying to call a non-const method.
Douglas Leeder
Yep, that turned out to be the solution. I had to do a const_cast.
fuenfundachtzig
Please don't do a const_cast - it means your code is broken and you're hacking around the problem. Either the method should be const, or the object pointer/reference shouldn't be const.
Douglas Leeder
Yes, I know it's evil -- but most of the code I cannot change. (I need to use a method which is non-const, and the only method which gives me a pointer to the object gives me a const pointer.)
fuenfundachtzig
+2  A: 

It is part of the overload resolution process.

The compiler lists all the overloads that were considered in this overload resolution so that you can check that the one you actually wanted to call is actually present (if not, you missed a header inclusion, a qualification etc...)

<near match> indicates that this is (according to the compiler) the possible overload you actually wanted to invoke since it is the best overload according to the criteria (const-ness, possible conversions, ...)

In other words, it is an indication that among all the overload considered, you probably wanted to call this and should check that your arguments actually match the signature.

Matthieu M.