链接列表错误:语法错误:缺少';'在标识符'head'之前
我必须为大学作业写一个链表,我已经按照我的说明遵循了对T的指示。链接列表错误:语法错误:缺少';'在标识符'head'之前
我想不通,为什么我得到的错误,它是与我listOfDouble类中声明的指针,
心中已经通过搜索有关该错误消息堆栈的答案和一些帖子建议这可能是我的#包括一个问题,我应该声明类:class ListOfDoubles;
我已经尝试更换我的包括无济于事。
我被困在这一点上,考虑到我是一名学生,所以我的错误可能最终变得微不足道。
编辑:这是微不足道的。
ListOfDoubles.h
#ifndef LISTOFDOUBLES_H
#define LISTOFDOUBLES_H
class ListOfDoubles{
public:
ListOfDoubles();
~ListOfDoubles();
void insert(double);
void displaylist();
double deleteMostRecent();
private:
DoubleListNodePtr head;
DoubleListNodePtr temp;
DoubleListNodePtr newNode;
};
#endif
DoubleListNode.h
#ifndef DOUBLELISTNODE_H
#define DOUBLELISTNODE_H
class DoubleListNode{
friend class ListOfDoubles;
public:
DoubleListNode(double);
private:
double data;
DoubleListNode *next;
};
typedef DoubleListNode* DoubleListNodePtr;
#endif
实施
#include "ListOfDoubles.h"
#include "DoubleListNode.h"
#include<iostream>
using std::cout;
ListOfDoubles::ListOfDoubles()
:head(NULL){
};
DoubleListNode::DoubleListNode(double data)
:data(data){
};
void ListOfDoubles::insert(double data){
newNode = new DoubleListNode(data);
newNode->next = head;
head = newNode;
};
void ListOfDoubles::displaylist(){
DoubleListNodePtr temp = head;
while (temp != NULL){
cout << temp->data;
temp = temp->next;
}
};
double ListOfDoubles::deleteMostRecent(){
};
ListOfDoubles::~ListOfDoubles(){
delete head;
delete temp;
delete newNode
};
主要
#include "ListOfDoubles.h"
#include "DoubleListNode.h"
#include<iostream>
int main()
{
ListOfDoubles list1;
list1.insert(25);
list1.displaylist();
system("pause");
return(0);
}
个
错误
1> main.cpp
1>d:\college\semester 5\algorithms and data structures\lab_6b\lab_6b\lab_6b\listofdoubles.h(13): error C2146: syntax error : missing ';' before identifier 'head'
1>d:\college\semester 5\algorithms and data structures\lab_6b\lab_6b\lab_6b\listofdoubles.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\college\semester 5\algorithms and data structures\lab_6b\lab_6b\lab_6b\listofdoubles.h(14): error C2146: syntax error : missing ';' before identifier 'temp'
1>d:\college\semester 5\algorithms and data structures\lab_6b\lab_6b\lab_6b\listofdoubles.h(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\college\semester 5\algorithms and data structures\lab_6b\lab_6b\lab_6b\listofdoubles.h(15): error C2146: syntax error : missing ';' before identifier 'newNode'
1>d:\college\semester 5\algorithms and data structures\lab_6b\lab_6b\lab_6b\listofdoubles.h(15): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
,而它在DoubleListNode.h定义,因此编译器不知道它是什么类型(“缺少类型说明”)你在ListOfDoubles.h使用DoubleListNodePtr。在ListOfDoubles.h中包含DoubleListNode.h。
正如在其他的答案
1.You提到需要包括DoubleListNode.h
在ListOfDoubles.h
2.You缺少 “;”在实施文件中的#34行。
delete newNode
应该
delete newNode;
是的,当我在ListOfDoubles.h中包含DoubleListNode.h后,发现我有点沮丧,我曾想过尝试一段时间,我完全忘记了。 – Johntk 2014-10-30 21:00:47
怎么样,包括'DoubleListNodePtr.h'在'ListOfDoubles.h'?或者至少为该指针提供前向声明和typedef? – 2014-10-30 20:46:30
没关系,我认为修复它。 – Johntk 2014-10-30 20:53:05