问题描述
是的,我知道这个问题以前已经被问过无数次了,但是由于我无法确定通常缺少的分号在哪里,所以我竭尽全力试图找出导致错误的原因.
Yes, I know this question has been asked numerous times previously, however I am racking my brain trying to figure out what is causing the error as I cannot identify where the usual missing semi-colon is.
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
推荐答案
这次不是缺少分号,而是一种完全不同的语言.您尝试编译的代码是C ++,而不是C.这就是为什么您从C编译器收到语法错误的原因.
This time it's not a missing semicolon, but a completely different language. The code you're trying to compile is in C++, not C. That's why you get syntax errors from a C compiler.
要解决此问题(并将代码编译为C ++),请将文件从 .c
重命名为 .cpp
:
To fix this (and compile the code as C++), rename the file from .c
to .cpp
:
mv list.c list.cpp
并使用 g ++
进行编译,而不是 gcc
:
And use g++
to compile it, not gcc
:
g++ -o list list.cpp
这篇关于错误:预期在"=",“,",“;","asm"或"__attribute__"之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!