错误:对的调用没有匹配的函数调用-使用VS2013编译

error: no matching function call for call to - compiles with VS2013 though

本文关键字:函数调用 使用 VS2013 编译 调用 错误      更新时间:2023-10-16

我正在编写一段应该在多个平台上运行的代码。我在使用Visual Studio 2013进行编译时,代码正常工作,没有任何问题,但现在我尝试为Android进行编译,我收到了标题中提到的错误。

我试图编译的代码是这样的:

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

错误中提到了上述方法。举个例子,我试着这样调用trim()方法:

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

将右值(如从substr()返回的临时值)传递给函数。

同样,仅仅传递价值不会再以这种方式工作了

static std::string trim(const std::string& s); 
               // ^ remove the reference

对于其他类似的功能,我建议您这样做。


或者使用左值调用函数

std::string part = line.substr(colon + 1);
return StringUtils::trim(part);