2014-10-10 108 views
0

我正在写一段代码,应该运行在多个平台上。我在使用Visual Studio 2013进行编译时没有任何问题就可以使用代码,但现在我尝试为Android编译它,但是在标题中提到了错误。错误:没有匹配函数调用呼叫 - 与VS2013编译虽然

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

#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>)' 

回答

0

你需要你的函数签名改为

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); 
相关问题