Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2015 "non-standard syntax; use '&' to create a pointer to member"

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?

like image 995
Lawrence Aiello Avatar asked Aug 18 '15 02:08

Lawrence Aiello


1 Answers

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; }
like image 124
songyuanyao Avatar answered Nov 06 '22 14:11

songyuanyao