【问题标题】:GNU g++ 4.9.2 Compilation Error for a find function call查找函数调用的 GNU g++ 4.9.2 编译错误
【发布时间】:2016-08-23 05:39:30
【问题描述】:

我将 StringVec 定义为:

typedef std::vector<string> StringVec;

变量 colnames 定义为:

StringVec colnames;

我有一个功能如下:

int colIndex(const string &cn) const {
        StringVec::iterator i1;
        i1 = find(colnames.begin(),colnames.end(),cn);
        return(i1 == colnames.end() ? -1 : (i1 - colnames.begin()));
}

当我尝试使用 GNU g++ 4.9.2 (C++11) 编译时,它会抱怨:

error: no matching function for call to 'find(std::vector<std::basic_string<char> >::const_iterator, std::vector<std::basic_string<char> >::const_iterator, const string&)'
 i1 = find(colnames.begin(),colnames.end(),cn);

即使std::find 也无法解决这个问题。 编译器给出了另一个线索:

note:   template argument deduction/substitution failed:
note:   '__gnu_cxx::__normal_iterator<const std::basic_string<char>*, std::vector<std::basic_string<char> > >' is not derived from 'std::istreambuf_iterator<_CharT>'
  i1 = std::find(colnames.begin(),colnames.end(),cn);

有什么线索吗?

【问题讨论】:

  • @ks1322 不,thst 没有解决,用编译器提供的更多线索编辑了我的 Qs
  • 您是否包含了查找功能的更正确的头文件?您应该创建一个完整但最小的示例来显示您遇到的错误,以便其他人可以复制/粘贴并自己尝试。
  • @nos,我添加了#include ,现在完美了!非常感谢。这与早期版本的编译器(非 C++11)完美编译,无论如何都很好

标签: c++ string c++11 vector find


【解决方案1】:

根据您提供的信息,我做了一个最小的示例(经过最小的修改,您是否包含标题?):

#include <string>
#include <algorithm>
#include <vector>

typedef std::vector<std::string> StringVec;
StringVec colnames;
int colIndex(const std::string &cn) {
        StringVec::iterator i1;
        i1 = std::find(colnames.begin(),colnames.end(),cn);
        return(i1 == colnames.end() ? -1 : (i1 - colnames.begin()));
}

int main() {

        return 0;
}

它使用 g++ 4.8.4 编译得很好:

gsamaras@gsamaras-A15:~$ g++ -Wall px.cpp 
gsamaras@gsamaras-A15:~$

【讨论】:

    猜你喜欢
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 2016-05-17
    • 1970-01-01
    相关资源
    最近更新 更多