链接列表错误:语法错误:缺少';'标识符'head'之前

时间:2014-10-30 20:44:42

标签: c++ pointers linked-list

我必须为大学作业编写一份链表,据我所知,我已经按照我的指示去了T.

我无法弄清楚为什么我会收到错误,这与我在listOfDouble类中声明的指针有关,

我搜索了有关错误消息的堆栈上的答案,并且有一些帖子表明这可能是我的#include的问题,而且我应该声明该类: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

2 个答案:

答案 0 :(得分:1)

你在ListOfDoubles.h中使用DoubleListNodePtr,而它在DoubleListNode.h中定义,因此编译器不知道它是什么类型(&#34;缺少类型说明符&#34; )。在ListOfDoubles.h中包含DoubleListNode.h。

答案 1 :(得分:0)

如其他答案所述

1.您需要在DoubleListNode.h

中加入ListOfDoubles.h

2.你缺少&#34;;&#34;在实施文件中的第34行。

delete newNode

应该是

delete newNode;
相关问题