GCC"没有匹配的呼叫功能.."错误

Mic*_* IV 4 c++ gcc visual-c++

我正在开发一个跨平台代码库,其中初始工作是使用MS VC2010编译器完成的.后来我在Linux上用GCC(4.7)编译它.在很多情况下我收到:

" GCC中没有匹配的函数调用.."错误.我注意到它主要是在方法参数非常常引用时抱怨.例如:

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


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

}
Run Code Online (Sandbox Code Playgroud)

一旦我将方法更改为:

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

}
Run Code Online (Sandbox Code Playgroud)

海湾合作委员会停止抱怨.为什么会发生这种情况,为什么VC编译器不会发生这种情况?

joh*_*ohn 9

将非const引用绑定到临时引用是非法的.然而,历史上VS编译器对此并不那么严格.

因此,如果你有一个带有非const引用的函数并且你用一个临时对象(例如函数的返回值)来调用它,那么g ++将会得到,但VS不会.在这种情况下,g ++是对的.

如果可以的话,总是喜欢const引用.