本文介绍了不完整的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于"next"和"previous"变量,我收到一个不完整的类型错误.我不确定自己在做什么错,因为我对使用C ++编写类非常生疏.任何帮助,将不胜感激!谢谢.
I'm getting a incomplete type error for the 'next' and 'previous' variables. I'm not sure what I am doing wrong because I am very rusty on writing classes in C++. Any help would be appreciated!Thanks.
#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;
}
推荐答案
您的类型具有递归定义,这是禁止的.
Your type has a recursive definition, which is prohibited.
class LinearNode
{
private:
LinearNode next;
LinearNode previous;
};
数据成员next
和previous
是LinearNode
类的实例(不是引用或指针),尚未完全定义.
The data members next
and previous
are instances (not references or pointers) of the LinearNode
class, which is not completely defined yet.
您可能想要这样:
class LinearNode
{
private:
LinearNode * next;
LinearNode * previous;
};
这篇关于不完整的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!