Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 40 characters in body
Source Link
outis
  • 77.7k
  • 23
  • 155
  • 228

I'm getting the following error from g++ while trying to add iterator support for my linked list class.

LinkedList.hpp: In member function ‘Type& exscape::LinkedList::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::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*’ 

Likely relevant code snippets:

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 } 

I've googled (and searched SO, of course), and checked my code to no avail - either I'm missing something basic (aka. doing something stupid), or I'm missing the knowledge required. Any advice?

I'm getting the following error from g++ while trying to add iterator support for my linked list class.

LinkedList.hpp: In member function ‘Type& exscape::LinkedList::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::iterator*’

Likely relevant code snippets:

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 } 

I've googled (and searched SO, of course), and checked my code to no avail - either I'm missing something basic (aka. doing something stupid), or I'm missing the knowledge required. Any advice?

I'm getting the following error from g++ while trying to add iterator support for my linked list class.

 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*’ 

Likely relevant code snippets:

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 } 

I've googled (and searched SO, of course), and checked my code to no avail - either I'm missing something basic (aka. doing something stupid), or I'm missing the knowledge required. Any advice?

tags do not belong to title
Link
Esko
  • 29.5k
  • 11
  • 58
  • 83

[C++] invalid initialization of non-const reference of type ‘int&' from a temporary of type 'MyClass<int>::iterator*'

Source Link
exscape
  • 2.2k
  • 4
  • 22
  • 25

[C++] invalid initialization of non-const reference of type ‘int&' from a temporary of type 'MyClass<int>::iterator*'

I'm getting the following error from g++ while trying to add iterator support for my linked list class.

LinkedList.hpp: In member function ‘Type& exscape::LinkedList::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::iterator*’

Likely relevant code snippets:

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 } 

I've googled (and searched SO, of course), and checked my code to no avail - either I'm missing something basic (aka. doing something stupid), or I'm missing the knowledge required. Any advice?