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

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

问题描述

我正在尝试在 C++ 中实现自己的链表,但终生无法弄清楚为什么会出现此错误.我知道有一个 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;
}

在指定的行上,我收到错误非标准语法;使用‘&’创建一个指向成员的指针".这到底是什么意思?

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天全站免登陆