我想做一個小的哈希程序,但有一個錯誤,我不知道如何處理。問題是上的箭頭,這裏是錯誤:迭代器錯誤C2440
Error 1 error C2440: 'initializing' : cannot convert from 'std::_String_const_iterator<_Elem,_Traits,_Alloc>' to 'std::_String_iterator<_Elem,_Traits,_Alloc>'
我的代碼:
#include <iostream>
#include <string>
#include <iterator>
using namespace std;
unsigned long hash(const string& str);
int main()
{
long out;
string word;
word = "about";
out = hash(word) % 255;
cout << out;
system("pause");
return 0;
}
unsigned long djb2(const string& str)
{
unsigned long hash = 5381;
for(string::iterator it=str.begin();it!=str.end();it++) //<~~~~~~~~~~
hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */
return hash;
}
無法將** _ String_const_iterator **轉換爲** _ String_iterator **。確保你徹底閱讀你的錯誤信息,他們是你的朋友! – Aesthete
沒有定義'unsigned long hash(const string & str);'。最重要的是,你已經命名了你的函數和變量是相同的,這並不是很好,我認爲'djb2()'函數應該是是'hash()'? – Aesthete