【问题标题】:error: no matching function call for call to - compiles with VS2013 though错误:没有匹配的函数调用调用 - 虽然用 VS2013 编译
【发布时间】:2014-10-10 10:07:01
【问题描述】:

我正在编写一段应该在多个平台上运行的代码。使用 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>)'

【问题讨论】:

    标签: c++


    【解决方案1】:

    您需要将函数签名更改为

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

    【讨论】:

      猜你喜欢
      • 2013-11-21
      • 1970-01-01
      • 1970-01-01
      • 2013-05-05
      • 1970-01-01
      • 2012-12-28
      • 2015-03-31
      • 1970-01-01
      相关资源
      最近更新 更多