错误:预期'=',',',';','asm'或'__attribute__'之前

时间:2017-05-04 02:01:21

标签: c list gcc

是的,我知道这个问题之前已被多次询问,但是我正在绞尽脑汁试图找出导致错误的原因,因为我无法确定通常缺少的分号在哪里。

list.h

/* list.h  */

#ifndef _LIST_H
#define _LIST_H

#define TRUE_ true
#define FALSE_ false

class LISTElement {
   public:
     LISTElement(void *itemPtr, int sortKey);  /* initialize a list element */
     LISTElement *next;         /* next element on list, 
                                   NULL if this is the last */
     int key;                   /* priority, for a sorted list */
     void *item;                /* pointer to item on the list */
};

class LIST {
  public:
    LIST();                     /* initialize the list  */
    ~LIST();                    /* de-allocate the list */

    bool IsEmpty();             /* is the list empty? */
    void Insert(void *item, int sortKey);       /* Put item into list */
    void *Remove(int *keyPtr);          /* Remove first item from list */

  private:
    LISTElement *first;         /* Head of the list, NULL if list is empty */
    LISTElement *last;          /* Last element of list */
};

#endif /* _LIST_H */

list.c

#include <stdlib.h>
#include "list.h"

LISTElement::LISTElement(void *itemPtr, int sortKey) {
     item = itemPtr;
     key = sortKey;
     next = NULL;       /* assume we'll put it at the end of the list */
}

LIST::LIST() {
    first = last = NULL;
}

LIST::~LIST() {
    while (Remove(NULL) != NULL)
        ;        // delete all the list elements
}

bool LIST::IsEmpty() {
    if (first == NULL)
        return TRUE_;
    else
        return FALSE_;
}

void LIST::Insert(void *item, int sortKey) {
    LISTElement *element = new LISTElement(item, sortKey);
    LISTElement *ptr;

    if (IsEmpty()) {    /* if list is empty, put */
        first = element;
        last = element;
    } else if (sortKey < first->key) {
                /* item goes on front of list */
        element->next = first;
        first = element;
    } else {            /* look for first element in list bigger than item */
        for (ptr = first; ptr->next != NULL; ptr = ptr->next) {
            if (sortKey < ptr->next->key) {
                element->next = ptr->next;
                ptr->next = element;
                return;
            }
        }
        last->next = element;           /* item goes at end of list */
        last = element;
    }
}

void * LIST::Remove(int *keyPtr) {
    LISTElement *element = first;
    void *thing;

    if (IsEmpty())
        return NULL;

    thing = first->item;
    if (first == last) {        /* list had one item, now has none */
        first = NULL;
        last = NULL;
    } else {
        first = element->next;
    }
    if (keyPtr != NULL)
        *keyPtr = element->key;
    delete element;
    return thing;
}

编译器抛出的错误:

$ gcc -o list list.c
In file included from list.c:2:
list.h:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘LISTElement’
list.h:18: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘LIST’
list.c:4: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token
list.c:10: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token
list.c:14: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token
list.c:19: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘LIST’
list.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token
list.c:50: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token

1 个答案:

答案 0 :(得分:1)

这一次,它不是一个缺少分号,而是一种完全不同的语言。您尝试编译的代码是使用C ++编写的,而不是C语言。这就是您从C编译器获得语法错误的原因。

要解决此问题(并将代码编译为C ++),请将文件从.c重命名为.cpp

mv list.c list.cpp

使用g++进行编译,而不是gcc

g++ -o list list.cpp