GNU g++ 4.9.2 查找函数调用的编译错误

GNU g++ 4.9.2 Compilation Error for a find function call

本文关键字:函数调用 编译 错误 查找 g++ GNU      更新时间:2023-10-16

我将StringVec定义为:

typedef std::vector<string> StringVec;

变量并列定义为:

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

有什么线索吗?

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

#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:~$