C++ 错误:没有与调用匹配的函数调用-但使用VS2013编译
我正在写一段应该在多个平台上运行的代码。我在使用Visual Studio 2013编译时代码正常工作,没有问题,但是现在我尝试为Android编译它,我得到了标题中提到的错误 我试图编译的代码如下所示:C++ 错误:没有与调用匹配的函数调用-但使用VS2013编译,c++,C++,我正在写一段应该在多个平台上运行的代码。我在使用Visual Studio 2013编译时代码正常工作,没有问题,但是现在我尝试为Android编译它,我得到了标题中提到的错误 我试图编译的代码如下所示: #pragma once #include <string> class StringUtils { public: static std::string readFile(const std::string& filename); static std
#pragma once
#include <string>
class StringUtils
{
public:
static std::string readFile(const std::string& filename);
static std::string& trimStart(std::string& s);
static std::string& trimEnd(std::string& s);
static std::string& trim(std::string& s);
};
std::string TRData::readValue(std::ifstream& ifs)
{
std::string line;
std::getline(ifs, line);
int colon = line.find_first_of(':');
assert(colon != std::string::npos);
return StringUtils::trim(line.substr(colon + 1));
}
错误消息指向此方法中的最后一行。我如何解决这个问题?正如我所说,它使用VS2013进行编译,但不适用于使用默认NDK工具链的Android
编辑:忘记粘贴准确的错误消息,如下所示:
error : no matching function for call to 'StringUtils::trim(std::basic_string<char>)'
您需要将函数签名更改为
static std::string& trim(const std::string& s);
// ^^^^^
将R值(如substr返回的临时值)传递给函数
而且,仅仅通过传递值将不再以这种方式工作
static std::string trim(const std::string& s);
// ^ remove the reference
我建议对其他类似的函数也这样做
或者使用左值调用函数
std::string part = line.substr(colon + 1);
return StringUtils::trim(part);