C++ 不完全类型

C++ 不完全类型,c++,incomplete-type,C++,Incomplete Type,“下一个”和“上一个”变量的类型错误不完整。我不确定我做错了什么,因为我在编写C++课程上很生疏。任何帮助都将不胜感激! 谢谢 #包括 使用名称空间std; 类线性节点 { 公众: //不带参数的LinearNode类的构造函数 线性节点(); //将元素作为参数的LinearNode类的构造函数 线性节点(int-el); //返回集合中的下一个节点。 LinearNode getNext(); //返回集合中的上一个节点 LinearNode getPrevious(); //设置集合中的下

“下一个”和“上一个”变量的类型错误不完整。我不确定我做错了什么,因为我在编写C++课程上很生疏。任何帮助都将不胜感激! 谢谢

#包括
使用名称空间std;
类线性节点
{
公众:
//不带参数的LinearNode类的构造函数
线性节点();
//将元素作为参数的LinearNode类的构造函数
线性节点(int-el);
//返回集合中的下一个节点。
LinearNode getNext();
//返回集合中的上一个节点
LinearNode getPrevious();
//设置集合中的下一个元素
void setNext(LinearNode节点);
//设置集合中的上一个元素
void setPrevious(LinearNode节点);
//设置节点的元素
无效集合元素(int-el);
//获取节点的元素
int getElement();
私人:
线性节点其次;
线性节点;
int元素;
};//结束LinearNode类
实施文件:

#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
//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
#包括
#包括“LinearNode.h”
使用名称空间std;
//LinearNode的构造函数,将next和元素设置为初始化状态
LinearNode::LinearNode()
{
next=NULL;
元素=0;
}//结束LinearNode默认构造函数
//LinearNode的构造函数将元素作为参数。
LinearNode::LinearNode(int-el)
{
next=NULL;
previous=NULL;
元素=0;
}//结束LinearNode构造函数
//返回结构中的下一个元素
LinearNode::getNext()
{
下一步返回;
}//结束getNext函数
//返回结构中的上一个元素
LinearNode::getPrevious()
{
返回上一个;
}//结束上一个函数
//设置节点的下一个变量
LinearNode::setNext(LinearNode节点)
{
下一个=节点
}//结束setNext函数
//为节点设置上一个
LinearNode::setPrevious(LinearNode节点)
{
上一个=节点;
}//结束setPrevious函数
//返回节点的元素
LinearNode::getElement()
{
返回元素;
}//结束getelement函数
//设置节点的元素
LinearNode::setElement(int-el)
{
元素=el;
}//结束setElement函数
测试文件:

    #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;

}
#包括
#包括“LinearNode.h”
使用名称空间std;
int main()
{
线性节点1,节点2,移动;
节点1.集合元素(1);
节点2.setElement(2);
node2.setNext(node1);
node1.setPrevious(node2);
移动=节点2;
while(move.getNext()!=NULL)

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


类型的线性节点的成员是实例,它们不允许在上下文中。记住,在C++中,如果声明“Fo-F”,则不是对C对象中堆对象的引用,而是在堆栈或类布局上的FO实例。

更糟糕的是,这两个LinearNode实例在其包含实例被实例化时会被实例化。它们中的每一个都有两个子实例,这两个子实例也会被实例化,并且每一个子实例都有两个……等等,依此类推,直到内存耗尽。当然,不允许这样做,因为这毫无意义


因此,它们必须是指针或引用。原因是编译器必须知道这两个实例的大小才能确定LinearNode类的大小,但在知道它们的大小之前,必须知道它们的大小。

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

class LinearNode
{
private: 
    LinearNode next;
    LinearNode previous;
};
数据成员
next
previous
LinearNode
类的实例(不是引用或指针),该类尚未完全定义

你可能想要这个:

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

似乎您正在尝试实现一个“链表”

链接的结构是下一个和上一个“点”(*),而不是Andre所说的另一个节点

可能有助于澄清

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;
};
因此,在实施文件中:

#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
//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
所以main看起来像:

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;

}
intmain()
{
线性节点1,节点2,移动;
节点1.集合元素(1);
节点2.setElement(2);
node2.setNext(&node1);//将节点的地址指定给下一个指针(&)
node1.setPrevious(&node2);//将节点的地址指定给下一个指针(&)
移动=节点2;
while(move.getNext()!=NULL)

请不要发布整个错误消息。这非常有帮助。很好地发现,这是一个额外的问题。我不想粗鲁或其他什么,但你只是在这里重新措辞了另外两个答案。此外,你的帖子似乎有一个断开的链接(可能无法定义它)。一点也不粗鲁,我只是试图把所有的tpar-链接修复