C++ 嵌套结构:定义结构指针时非静态成员的使用无效

C++ 嵌套结构:定义结构指针时非静态成员的使用无效,c++,struct,nested,static-members,C++,Struct,Nested,Static Members,这是一个链接列表的演示,我定义了一个节点结构,它的指针是head,但编译器说:in--在以下位置有效使用非静态成员: Node* head; 此外,如果我不预先声明struct节点,它将报告未声明的节点 代码如下: #ifndef LINKLIST_H #define LINKLIST_H template<typename T> class LinkList { struct Node; //why declaration is required here

这是一个链接列表的演示,我定义了一个节点结构,它的指针是head,但编译器说:in--在以下位置有效使用非静态成员:

Node* head;
此外,如果我不预先声明
struct节点
,它将报告未声明的节点

代码如下:

#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;   
   //.....
      
\ifndef链接列表
#定义链接列表
模板
类链接列表
{
struct Node;//为什么这里需要声明???
公众:
//成员函数
节点*Find(tx);
//.....
私人:
结构体类型
{
T数据;
节点*下一步;
//Node():下一个(NULL){}
节点(const T&d=0,Node*n=NULL):数据(d),下一个(n){
};
Node*head;//错误?????为什么?
};
模板
typename链接列表::节点*链接列表::查找(T x)
{
节点*ptr=head->next;
//.....
}

endif//LINKLIST\u H 运行时错误:

||=== Build: Release in Broken Keyboard (compiler: GNU GCC Compiler) ===|
include\LinkList.h|41|error: invalid use of non-static data member 'LinkList<T>::head'|
include\LinkList.h|22|error: from this location|
include\LinkList.h|41|error: invalid use of non-static data member 'LinkList<T>::head'|
include\LinkList.h|95|error: from this location|
include\LinkList.h|95|error: default argument given for parameter 1 of 'void LinkList<T>::Insert(T, LinkList<T>::Node*)' [-fpermissive]|
include\LinkList.h|22|error: after previous specification in 'void LinkList<T>::Insert(T, LinkList<T>::Node*)' [-fpermissive]|
include\LinkList.h|95|error: default argument given for parameter 2 of 'void LinkList<T>::Insert(T, LinkList<T>::Node*)'|
include\LinkList.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)) ===|
||===构建:在损坏的键盘中发布(编译器:GNU GCC编译器)===|
include\LinkList.h | 41 |错误:非静态数据成员'LinkList::head'的使用无效|
include\LinkList.h | 22 |错误:来自此位置|
include\LinkList.h | 41 |错误:非静态数据成员'LinkList::head'的使用无效|
include\LinkList.h | 95 |错误:来自此位置|
include\LinkList.h | 95 |错误:为“void LinkList::Insert(T,LinkList::Node*)”的参数1提供了默认参数[-fppermissive]|
include\LinkList.h | 22 |错误:在“void LinkList::Insert(T,LinkList::Node*)”中先前的规范之后[-fppermissive]|
include\LinkList.h | 95 |错误:为“void LinkList::Insert(T,LinkList::Node*)”的参数2提供了默认参数|
include\LinkList.h | 22 |错误:在“void LinkList::Insert(T,LinkList::Node*)中先前的规范之后”|
||==生成失败:8个错误,0个警告(0分钟,0秒))===|

由于在提供节点定义之前在Find方法中使用struct Node,因此需要struct Node的第一个正向声明。 但您不应该在Node*head中出现错误。我已经在Visual Studio 2015中尝试了您的代码,并在main中实例化了模板,没有错误

你的编译器版本是什么


另外。

由于在提供节点定义之前在Find方法中使用struct Node,因此需要struct Node的第一个正向声明。 但您不应该在Node*head中出现错误。我已经在Visual Studio 2015中尝试了您的代码,并在main中实例化了模板,没有错误

你的编译器版本是什么


Alon.

请提供整个错误消息。我添加了它。请查看。谢谢。请提供整个错误消息。我添加了它。请看一看。谢谢。我使用代码块,模板类在单独的.h文件中声明和实现。当我在main.cpp中尝试它时,它运行正常。真奇怪!谢谢你的回复!我使用代码块,模板类在单独的.h文件中声明和实现。当我在main.cpp中尝试它时,它运行正常。真奇怪!谢谢你的回复!