这里是我的代码:收到错误消息说变量没有被宣布时,它具有
struct data {
char carReg[6];
char make[20], model[20], colour[20];
int numPrevOwners;
bool reserved;
float reserveAmount;
};
struct node {
struct data *element;
struct node *next;
};
而且在我的主要方法,我有这样的:
struct node *current, *aNode;
struct data *anElement, *newCar;
FILE *file;
file = fopen("d:car.dat", "r"); //open the file for reading
if (file == NULL) {
printf("Nothing found in this file.\n\n");
printf("\n");
}//end if
else {
//If it does exist, cars in file should be copied into the linked list
while(fread(&newCar, sizeof(struct data), 1, file)>0) {
aNode = (struct node *)malloc(sizeof(struct node));
anElement = (struct data *)malloc(sizeof(struct data));
strcpy(anElement->carReg ,newCar->carReg);
strcpy(anElement->make, newCar->make);
strcpy(anElement->model, newCar->model);
strcpy(anElement->colour, newCar->colour);
anElement->numPrevOwners = newCar->numPrevOwners;
anElement->reserved = newCar->reserved;
anElement->reserveAmount = newCar->reserveAmount;
if (aNode == NULL)
printf("Error - no space for the new node\n");
else { // add data part to the node
aNode->element = anElement;
aNode->next = NULL;
if (isEmpty())
{
front = aNode;
last = aNode;
}
else {
last->next = aNode;
last = aNode;
}
}
}//end while
printf("Cars in the system");
}//end else
错误消息我m得到是'carReg'尚未申报,'make'尚未申报等。
任何人都可以帮忙吗?
编辑 - 我有它更新,它都编译,但程序不运行。它运行但说title.exe已停止运行。
你需要所有指针代替'''',而不是''',而不仅仅是其中的一些 – deviantfan
'newCar'不应该是一个指针。目前你的代码并没有保存'aNode'或'anElement'中的任何指针。 – ooga
@ooga我上面编辑了我的评论。它编译但不能正常运行。 – jf95