Visual Studio 2015 "non-standard syntax; use '&' to create a pointer to member"
Visual Studio 2015 "non-standard syntax; use '&' to create a pointer to member"
我正在尝试用 C++ 实现自己的链表,但我终究无法弄清楚为什么会出现此错误。我知道有一个 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;
}
在指定的行上,我收到错误 "non-standard syntax; use '&' to create a pointer to member"。这到底是什么意思?
您正在尝试 return 成员函数 value
,而不是成员变量 data
。变化
const T& value() const { return value; }
到
const T& value() const { return data; }
这里的问题是模板的混乱组合、运算符的多重含义和拼写错误。
您有一个方法 returning a const T&
,其中 T
是模板参数。您打算 return 对类型 T
的对象的常量引用。但是由于打字错误,编译器认为您正在尝试将其与方法 (value
) 匹配,而不是 T
类型的对象 (data
)。编译器认为您正在尝试使用 &
作为成员指针运算符,而不是对象引用运算符。但它足够聪明,有些事情不太对劲,因此发出警告。
我正在尝试用 C++ 实现自己的链表,但我终究无法弄清楚为什么会出现此错误。我知道有一个 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;
}
在指定的行上,我收到错误 "non-standard syntax; use '&' to create a pointer to member"。这到底是什么意思?
您正在尝试 return 成员函数 value
,而不是成员变量 data
。变化
const T& value() const { return value; }
到
const T& value() const { return data; }
这里的问题是模板的混乱组合、运算符的多重含义和拼写错误。
您有一个方法 returning a const T&
,其中 T
是模板参数。您打算 return 对类型 T
的对象的常量引用。但是由于打字错误,编译器认为您正在尝试将其与方法 (value
) 匹配,而不是 T
类型的对象 (data
)。编译器认为您正在尝试使用 &
作为成员指针运算符,而不是对象引用运算符。但它足够聪明,有些事情不太对劲,因此发出警告。