Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 365
Michael IV Avatar asked Jan 13 '23 06:01

Michael IV


1 Answers

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.

like image 80
john Avatar answered Jan 17 '23 17:01

john