Visual Studio 2015“非标准语法;使用'&'以创建指向成员的指针“ [英] Visual Studio 2015 "non-standard syntax; use '&' to create a pointer to member"

查看:1449
本文介绍了Visual Studio 2015“非标准语法;使用'&'以创建指向成员的指针“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中尝试我自己的Linked List实现,并且不能为我的生活找出为什么我有这个错误。我知道有一个STL实现,但由于我正在尝试我自己的。这里是代码:

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;
}

在指定的行,我得到错误non-standard syntax; use '&'创建指向成员的指针。这是什么意思?

On the specified line, I get the error "non-standard syntax; use '&' to create a pointer to member". What the hell does this mean?

推荐答案

您试图返回成员函数 value ,而不是成员变量 data 。变更

You're trying to return the member function value, not the member variable data. Change

const T& value() const { return value; }

const T& value() const { return data; }

这篇关于Visual Studio 2015“非标准语法;使用'&amp;'以创建指向成员的指针“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆