嵌套结构:在定义结构指针时使用了无效的非静态成员
Nested struct: with invalid use of non-static member when define struct pointer
这是一个链表的演示,我定义了一个Node结构,它的指针是head,但编译器说:in——在这个位置有效使用非静态成员:
Node* head;
此外,如果我不预先声明struct Node
,它将报告未声明的Node。
代码如下:
#ifndef LINKLIST_H
#define LINKLIST_H
template<typename T>
class LinkList
{
struct Node; //why declaration is required here ???
public:
// member function
Node* Find(T x);
//.....
private:
struct Node
{
T data;
Node* next;
// Node():next(NULL){}
Node(const T& d=0, Node* n=NULL):data(d),next(n){}
};
Node* head; //ERROR ?????? why?
};
template<typename T>
typename LinkList<T>::Node* LinkList<T>::Find(T x)
{
Node* ptr=head->next;
//.....
}
endif//LINKLIST_H
运行时错误:
||=== Build: Release in Broken Keyboard (compiler: GNU GCC Compiler) ===|
includeLinkList.h|41|error: invalid use of non-static data member 'LinkList<T>::head'|
includeLinkList.h|22|error: from this location|
includeLinkList.h|41|error: invalid use of non-static data member 'LinkList<T>::head'|
includeLinkList.h|95|error: from this location|
includeLinkList.h|95|error: default argument given for parameter 1 of 'void LinkList<T>::Insert(T, LinkList<T>::Node*)' [-fpermissive]|
includeLinkList.h|22|error: after previous specification in 'void LinkList<T>::Insert(T, LinkList<T>::Node*)' [-fpermissive]|
includeLinkList.h|95|error: default argument given for parameter 2 of 'void LinkList<T>::Insert(T, LinkList<T>::Node*)'|
includeLinkList.h|22|error: after previous specification in 'void LinkList<T>::Insert(T, LinkList<T>::Node*)'|
||=== Build failed: 8 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
structNode的第一个正向声明是必需的,因为在提供Node定义之前,您在Find方法中使用它。但是您不应该在Node*头中出现错误。我已经在Visual Studio 2015中尝试了您的代码,并在main中实例化了模板,没有错误。
你的编译器版本是什么?
阿隆。
相关文章:
- 在"结构提升::enable_if<提升::is_pod<T>,无效>"中没有名为"type"的类型
- 在另一个结构中声明内部结构会导致错误:结构使用无效
- 结构作为参数的函数产生错误:free():无效指针:0x00007efd47b
- 从 WindowsAPI 调用 NetLocalGroupAddMembers 时,安全 ID 结构无效错误
- 在结构中访问数组时无效使用非静态数据成员
- 无效操作数以二进制读取结构的阵列
- C++ 无效的比较器排序结构向量
- Valgrind 抱怨通过指向结构的指针访问结构成员时读取无效
- 具有空的(无效)结构的平等
- “无效的操作数是二进制表达式”当使用自定义结构作为C 中的MAP索引时
- 结构的前向声明无效
- 结构sqlite3的sqlite3c++正向声明导致析构函数中删除时出现无效指针错误
- 如何判断我的结构tm是否处于无效状态
- 嵌套结构:在定义结构指针时使用了无效的非静态成员
- 具有结构的映射的前面的声明符无效
- 使用指向结构数组的指针的参数类型无效
- 尝试将结构复制到设备内存时 CUDA 参数无效 (cudaMemcpy)
- 获取类型的错误 无效使用不完整类型的"结构节点"
- 命名空间 ;")"令牌之前的预期非限定 ID ;结构使用无效
- 在哪些体系结构上计算无效指针不安全