我正在为学校做作业,我遇到了一个我似乎无法摆脱的重复错误。我的代码中不断重复以下错误
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 "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:
};
答案 0 :(得分:0)
错误消息的原因是您没有任何名为struct node
的类型。所以你不能new
struct node
。
对我而言,似乎主要问题是您混合了C
和C++
语法。在C
中,正确的语法是重复该类型的struct
部分,但不会重复C++
struct
。
所以修复只是替换
struct node
带
Node
文件中的所有位置。
除此之外,您有很多地方可以将tmp
与temp
混为一起
你也遇到了问题
Node* DLinkedList::removeNode(Point XY)
如你所说,该函数将返回Node*
,但在函数内部,你调用return
没有参数。
你可能想要
void DLinkedList::removeNode(Point XY)