gpt4 book ai didi

c++ - 不完整类型

转载 作者:太空狗 更新时间:2023-10-29 23:24:19 25 4
gpt4 key购买 nike

我收到“下一个”和“上一个”变量的不完整类型错误。我不确定我做错了什么,因为我对用 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;

}

最佳答案

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

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

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

你可能想要这个:

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

关于c++ - 不完整类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5276134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com