代码之家  ›  专栏  ›  技术社区  ›  Dr. Debasish Jana

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

  •  1
  • Dr. Debasish Jana  · 技术社区  · 9 年前

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

    有线索吗?

    1 回复  |  直到 9 年前
        1
  •  3
  •   gsamaras a Data Head    9 年前

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

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