Law*_*llo 7 c++ templates linked-list visual-studio-2015
我正在尝试用C++编写自己的Linked List实现,并且在我的生活中无法弄清楚为什么我会遇到这个错误.我知道有一个STL实现,但由于我正在尝试自己的原因.这是代码:
#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;
}
Run Code Online (Sandbox Code Playgroud)
在指定的行上,我得到错误"非标准语法;使用'&'创建指向成员的指针".这到底是什么意思?
您正在尝试返回成员函数value,而不是成员变量data.更改
const T& value() const { return value; }
Run Code Online (Sandbox Code Playgroud)
至
const T& value() const { return data; }
Run Code Online (Sandbox Code Playgroud)