我正在开发一个跨平台代码库,其中初始工作是使用 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);
}
一旦我将方法更改为:
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 停止提示。 为什么在 VC 编译器中会发生,为什么不会发生?
最佳答案
将非常量引用绑定(bind)到临时引用是非法的。然而,从历史上看,VS 编译器对此并不严格。
因此,如果您有一个带有非常量引用的函数,并且您使用一个临时对象(例如函数的返回值)调用它,g++ 会兼容,但 VS 不会。在这种情况下,g++ 是正确的。
如果可以的话,总是喜欢 const 引用。
关于c++ - 海湾合作委员会 "no matching function for call.."错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19090998/