我在嘗試爲我的鏈接列表類添加迭代器支持時從g ++收到以下錯誤。從'MyClass'類型的臨時類型的非常量引用類型'int&'的無效初始化<int> :: iterator *'
LinkedList.hpp: In member function ‘Type& exscape::LinkedList<Type>::iterator::operator*() [with Type = int]’: tests.cpp:51: instantiated from here LinkedList.hpp:412: error: invalid initialization of non-const reference of type ‘int&’ from a temporary of type ‘exscape::LinkedList<int>::iterator*’
可能的相關代碼片段:
LinkedList.hpp:
template <typename Type>
class LinkedList {
private:
struct node {
struct node *prev;
struct node *next;
Type data;
};
public:
class iterator : public std::iterator<...> {
node *p;
public:
Type &operator*();
};
...
};
template <typename Type>
LinkedList<Type>::iterator::iterator(struct node *in_node) : p(in_node) {}
template <typename Type>
inline Type &LinkedList<Type>::iterator::operator*() {
return this-p->data; ///// Line 412
}
tests.cpp:
...
LinkedList<int> l1;
...
LinkedList<int>::iterator it;
for (it = l1.begin(); it != l1.end(); ++it) {
std::cout << "Element: " << *it << std::endl; ///// Line 51
}
我GOOGLE了(因此搜索的,當然),並檢查我的代碼無濟於事 - 要麼我失去了一些基本的東西(又名一些愚蠢的東西),或者我錯過了所需的知識。有什麼建議?
看起來你可能會對線一個錯字412 – 2009-11-20 19:55:43