Michael IV
Michael IV

Reputation: 11486

GCC "no matching function for call.." errors

I am developing a cross platform code base where the initial work is done using MS VC2010 compiler.Later I compile it on Linux with GCC (4.7).In many cases I am receiving :

"No matching function for call .." error in GCC.I noticed that it complains mostly when method params are non constant references.For example this:

 void MyClass::DoSomeWork(ObjectSP &sprt, const std::string someName, const std::string anotherName, const std::string path, int index) {


        sprt->GetProp()->Update(path, false);

}

Once I change the method to this:

 void MyClass::DoSomeWork(const ObjectSP& sprt, const std::string& someName, const std::string& anotherName, const std::string& path, int index) {


        sprt->GetProp()->Update(path, false);

}

GCC stops complaining. Why does it happen and why does it not happen in VC compilers?

Upvotes: 4

Views: 3579

Answers (1)

john
john

Reputation: 87932

It's illegal to bind a non-const reference to a temporary. Historically however VS compilers have been less strict about this.

So if you have a function with a non-const reference and you call it with a temporary object (e.g. the return value from a function), g++ will compain, but VS won't. In this case g++ is right.

Always prefer const references if you can.

Upvotes: 9

Related Questions