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“非标准语法;使用“&"创建一个指向成员的指针"


基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01