2016-04-28 52 views
1

我已經StringVec定義爲:GNU克++用於查找函數調用4.9.2編譯錯誤

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克編譯++ 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

'std :: find()'? – ks1322

+0

@ ks1322沒有,thst沒有解決,編輯我的Qs與編譯器的更多線索 –

+2

您是否包含finder函數的proer頭文件?你應該創建一個完整但很小的例子來顯示你有錯誤,所以其他人可以複製/粘貼並自己嘗試。 – nos

回答

3

隨着你給的信息,我做了一個小例子(用最小的修改,你包括標題):

#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罰款:

[email protected]:~$ g++ -Wall px.cpp 
[email protected]:~$