Visual Studio 2015 quot;non-standard syntax; use #39;amp;#39; to create a pointer to memberquot;(Visual Studio 2015“非标准语法;使用“amp;创建一个指向成员的指针)
问题描述
我正在尝试在 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“非标准语法;使用“&"创建一个指向成员的指针"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Visual Studio 2015“非标准语法;使用“&"创建一个指向成员的指针"
基础教程推荐
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
