我正嘗試創建自己的翻譯器。這是大學工作。我需要在我的類翻譯器中使用迭代器。C++中的迭代器
class Translator
{
private:
map <string,Word> translator;
public:
class iterator
{
friend class Translator;
private:
map<string,Word>::iterator itm;
public:
iterator operator++();
pair <string,Word> &operator*();
bool operator==(const iterator &it)const;
};
};
我想超載operator*()
;
這是代碼。
pair <string, Word>& Translator::iterator::operator*()
{
return (*itm);
}
錯誤:
invalid initialization of reference of type ‘std::pair<std::basic_string<char>, Word>&’ from expression of type ‘std::pair<const std::basic_string<char>, Word>
順便說一句,你可能想打電話少的東西'translator'。只是爲了讓你的代碼更容易談論/推理。 ;) – Yakk 2014-12-02 10:06:21