不完整的类型

时间:2011-03-11 17:20:56

标签: c++ incomplete-type

我为'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;

}

4 个答案:

答案 0 :(得分:14)

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

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

数据成员nextpreviousLinearNode类的实例(不是引用或指针),尚未完全定义。

你可能想要这个:

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

答案 1 :(得分:4)

您需要为所有.cpp函数指定返回类型。 例如:

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

答案 2 :(得分:0)

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

更糟糕的是,当实例化包含实例时,这两个LinearNode实例将实例化。并且它们中的每一个都有两个子实例,它们同样会被实例化,并且每个实例都有两个......依此类推,依此类推,直到你的内存不足为止。当然,你不被允许这样做,因为它毫无意义。

所以那些必须是指针或引用。原因是编译器必须知道这两个实例的大小才能确定LinearNode类的大小,但必须知道该大小才能知道它们的大小。

答案 3 :(得分:0)

看起来你试图捏造一个“链表”

链接的结构是下一个&amp;以前的“点”(*)并不是安德烈所说的其他节点。

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。

相关问题