C++ 未解决的对节点无效类型的不完整使用
我正在为学校做一项作业,我不断犯一个我似乎无法摆脱的错误。以下错误在我的代码中不断重复C++ 未解决的对节点无效类型的不完整使用,c++,C++,我正在为学校做一项作业,我不断犯一个我似乎无法摆脱的错误。以下错误在我的代码中不断重复 hw_9_1.cpp: In member function ‘void DLinkedList::addNode(Node*)’: hw_9_1.cpp:32:31: error: invalid use of incomplete type ‘struct DLinkedList::addNode(Node*)::node’ temp = new(struct node);
hw_9_1.cpp: In member function ‘void DLinkedList::addNode(Node*)’:
hw_9_1.cpp:32:31: error: invalid use of incomplete type ‘struct DLinkedList::addNode(Node*)::node’
temp = new(struct node);
^
hw_9_1.cpp:32:27: note: forward declaration of ‘struct DLinkedList::addNode(Node*)::node’
temp = new(struct node);
^~~~
hw_9_1.cpp:33:15: error: request for member ‘xy’ in ‘* temp’, which is of non-class type ‘int’
temp->xy = node->xy;
^~
hw_9_1.cpp:33:24: error: expected primary-expression before ‘->’ token
temp->xy = node->xy;
^~
hw_9_1.cpp:34:15: error: request for member ‘next’ in ‘* temp’, which is of non-class type ‘int’
temp->next = NULL;
有人告诉我,这通常是由于缺少一个include文件,但我已经检查了多次,以确保所有必要的文件都包含在内。谁能告诉我是什么导致了这些错误
与此问题相关的作业代码如下
#include "std_lib_facilities_4.h"
#include "Simple_window.h"
#include "Graph.h"
struct Node {
Point xy;
Node* next;
Node* prev;
}*start;
class DLinkedList {
public:
Node* head;
Node* tail;
DLinkedList() : head(nullptr), tail(nullptr) {}
void addNode(Node*); // add a Node to the list
Node* removeNode(Point); //remove a Node from the list
};
void DLinkedList::addNode(Node* node) {
if (start == NULL)
{
head=start;
struct node *s, *temp;
temp = new(struct node);
temp->xy = node->xy;
temp->prev=NULL;
temp->next = NULL;
}
struct node *tmp, *q;
int i;
head=start;
temp = new(struct node);
tmp->xy = node->xy;
q->next=tail
if (q->next == NULL)
{
q->next = tmp;
tmp->next = NULL;
tmp->prev = q;
tail->next=temp;
}
}
Node* DLinkedList::removeNode(Point XY) {
struct node *tmp, *q;
/*first element deletion*/
if (start->xy == XY)
{
tmp = start;
start = start->next;
start->prev = NULL;
free(tmp);
return;
}
q = start;
while (q->next->next != NULL)
{
/*Element deleted in between*/
if (q->next->xy == XY)
{
tmp = q->next;
q->next = tmp->next;
tmp->next->prev = q;
free(tmp);
return;
}
q = q->next;
}
/*last element deleted*/
if (q->next->xy == XY)
{
tmp = q->next;
free(tmp);
q->next = NULL;
return;
}
}
struct DLL : Shape { // Doubly Linked List
void add(Point p) { Shape::add(p); }
void set_point(int i,Point p) { Shape::set_point(i,p); }
void draw_lines() const;
Point last_removed_point;
DLinkedList this_list;
private:
};
出现错误消息的原因是您没有任何名为
struct node
的类型。因此,您不能new
astruct节点
在我看来,主要的问题是混合了C
和C++
语法。在C
中,正确的语法是重复类型的struct
部分,但在C++
struct
中不重复
因此,解决办法只是更换
struct node
与
文件中的所有位置
除此之外,您还有许多地方将tmp
与temp
你也有问题
Node* DLinkedList::removeNode(Point XY)
正如您所说,函数将返回一个节点*
,但在函数内部,您调用return
时不带任何参数
你可能想要
void DLinkedList::removeNode(Point XY)
什么是
struct node
,如果有的话,它与struct node
有什么关系?您似乎混合了c
和c++
语法。尝试Node
而不是struct Node
void DLinkedList::removeNode(Point XY)