不完全类型

5

我在使用'next'和'previous'变量时遇到了不完整类型错误。由于我很生疏C++中的类编写,所以不确定自己做错了什么。希望能得到帮助!谢谢。

#include<iostream>

using namespace std;

class LinearNode
{
    public:
        //Constructor for the LinearNode class that takes no arguments 
        LinearNode();
        //Constructor for the LinearNode class that takes the element as an argument
        LinearNode(int el);
        //returns the next node in the set.
        LinearNode getNext();
        //returns the previous node in the set
        LinearNode getPrevious();
        //sets the next element in the set
        void setNext(LinearNode node);
        //sets the previous element in the set
        void setPrevious(LinearNode node);
        //sets the element of the node
        void setElement(int el);
        //gets the element of the node
        int getElement();

    private: 
        LinearNode next;
        LinearNode previous;
        int element;        
};//ends the LinearNode class

实现文件:

#include<iostream>
#include"LinearNode.h"

using namespace std;

//Constructor for LinearNode, sets next and element to initialized states
LinearNode::LinearNode()
{
    next = NULL;
    element = 0;
}//ends LinearNode default constructor

//Constructor for LinearNode takes an element as argument.
LinearNode::LinearNode(int el)
{
    next = NULL;
    previous = NULL;
    element = 0;
}//ends LinearNode constructor

//returns the next element in the structure
LinearNode::getNext()
{
    return next;
}//ends getNext function

//returns previous element in structure
LinearNode::getPrevious()
{
    return previous;
}//ends getPrevious function

//sets the next variable for the node
LinearNode::setNext(LinearNode node)
{
    next = node
}//ends the setNext function

//sets previous for the node
LinearNode::setPrevious(LinearNode node)
{
    previous = node;
}//ends the setPrevious function

//returns element of the node
LinearNode::getElement()
{
    return element;
}//ends the getelement function

//sets the element of the node
LinearNode::setElement(int el)
{
    element = el;
}//ends the setElement function

测试文件:

    #include<iostream>
#include"LinearNode.h"

using namespace std;

int main()
{
    LinearNode node1, node2, move;
    node1.setElement(1);
    node2.setElement(2);

    node2.setNext(node1);
    node1.setPrevious(node2);

    move = node2;

    while(move.getNext() != NULL)
        cout << move.getElement() << endl;

}

2
请发布完整的错误信息。这非常有帮助。 - James
4个回答

14

您的类型具有递归定义,这是被禁止的。

class LinearNode
{
private: 
    LinearNode next;
    LinearNode previous;
};

nextpreviousLinearNode类的实例(而不是引用或指针),该类尚未完全定义。

你可能想要这个:

class LinearNode
{
private: 
    LinearNode * next;
    LinearNode * previous;
};

4
您需要为所有 .cpp 函数指定返回类型。 例如:
    //returns previous element in structure
LinearNode LinearNode::getPrevious()
{
    return previous;
}//ends getPrevious function

1
很好,你发现了一个额外的问题。 - André Caron

0

看起来你正在尝试实现一个“链表”。

链表的结构是下一个和上一个“指向”(*),并不是像安德烈所说的另一个节点。

http://www.cplusplus.com/doc/tutorial/pointers/ 可能会有所帮助。

class LinearNode
{
//...snip
//returns the next node in the set.
LinearNode* getNext();
//returns the previous node in the set
LinearNode* getPrevious();

//...snip
private: 
    LinearNode * next;
    LinearNode * previous;
};

所以在实现文件中:

//returns the next element in the structure
LinearNode* LinearNode::getNext()
{
    return next; // now a poiner
}//ends getNext function

//returns previous element in structure
LinearNode*  LinearNode::getPrevious()
{
    return previous; // now a poiner
}//ends getPrevious function

//sets the next variable for the node
void LinearNode::setNext(LinearNode* node) //pointer in, void out
{
    next = node
}//ends the setNext function

//sets previous for the node
void LinearNode::setPrevious(LinearNode* node)//pointer in, void out
{
    previous = node;
}//ends the setPrevious function

所以主函数看起来像这样:

int main()
{
    LinearNode node1, node2, move;
    node1.setElement(1);
    node2.setElement(2);

    node2.setNext(&node1); // give the address of node to the next pointer (&)
    node1.setPrevious(&node2); // give the address of node to the next pointer (&)

    move = node2;

    while(move.getNext() != NULL)
        cout << move.getElement() << endl;

}

我不知道 while 循环想要做什么!如果 - 也许?

另外,正如 Nicklamort 所述,除了 void 的情况外,所有类函数都需要有返回类型。


我不是要无礼或者什么的,但你只是重新表述了其他两个答案。另外,你的帖子似乎有一个损坏的链接(可能没有定义它)。 - André Caron
一点也不粗鲁,我只是想把所有东西都整合起来给tpar看 - 链接已修复。 - Brandrew

0

你的 LinearNode 类型成员是实例,而在此上下文中它们不允许是实例。请记住,在 C++ 中,如果你声明 "Foo f",那不是像 C# 中引用堆对象的引用,而是在栈或类布局中的Foo实例。

更糟糕的是,这两个 LinearNode 实例将在它们所包含的实例被实例化时被实例化。每个实例都有两个子实例,这些子实例也将被实例化,每个子实例都有两个...依此类推,直到内存耗尽。自然地,你不能这样做,因为这没有意义。

所以这些必须是指针或引用。原因是编译器必须确定LinearNode类的大小,但是需要在确定其大小之前知道这两个实例的大小。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接