I am attempting my own Linked List implementation in C++ and cannot for the life of me figure out why I am having this error. I know there is an STL implementation but for reasons I am trying my own. Here is the code:
#include <iostream>
template <class T>
class ListElement {
public:
    ListElement(const T &value) : next(NULL), data(value) {}
    ~ListElement() {}
    ListElement *getNext() { return next; }
    const T& value() const { return value; }
    void setNext(ListElement *elem) { next = elem; }
    void setValue(const T& value) { data = value; }
private:
    ListElement* next;
    T data;
};
int main()
{
    ListElement<int> *node = new ListElement<int>(5);
    node->setValue(6);
    std::cout << node->value(); // ERROR
    return 0;
}
On the specified line, I get the error "non-standard syntax; use '&' to create a pointer to member". What the hell does this mean?
You're trying to return the member function value, not the member variable data. Change
const T& value() const { return value; }
to
const T& value() const { return data; }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With