2011-10-10 83 views
0

好了,所以我得到这些错误在用gcc编译:故障排除编译时间循环链表错误

prelab6.h: In function âinsertHeadCircularâ: 
prelab6.h:45: error: incompatible types in assignment 
prelab6.h:46: error: incompatible types in assignment 
prelab6.c: At top level: 
prelab6.c:41: warning: data definition has no type or storage class 
prelab6.c:41: warning: parameter names (without types) in function declaration 
prelab6.c:41: error: conflicting types for âprintInOrderâ 
prelab6.h:81: error: previous definition of âprintInOrderâ was here 
prelab6.c:42: warning: data definition has no type or storage class 
prelab6.c:42: warning: parameter names (without types) in function declaration 
prelab6.c:42: error: conflicting types for âprintReverseâ 
prelab6.h:112: error: previous definition of âprintReverseâ was here 

我已经试了又试,但无济于事修正这些错误。感谢任何和所有的帮助。

这是我的.c文件:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include "my.h" 

int main (int argc, char **argv) 
{ 

char firstname[100]; 
char lastname[100]; 
int monthsEmployed; 

FILE *fptr; 
fptr = fopen(argv[1], "r"); 

if (fptr == NULL) 
    printf ("Incorrect file reading!"); 

if (argc != 2) 
    printf ("Incorrect number of arguments!"); 


employeeInfo *insert; 
insert = malloc(sizeof(employeeInfo)); 
employeeList *head; 
head = NULL; 


while(!feof(fptr)) 
{ 
    fscanf (fptr, "%100s %100s %d", firstname, lastname, &monthsEmployed); 

    strcpy(insert->firstname, firstname); 
    strcpy(insert->lastname, lastname); 
    insert->monthsEmployed = monthsEmployed; 

    head = insertHeadCircular(head, insert); 
} 
} 

printInOrder(head); // display the linked list 
printReverse(head); // display the linked list in reverse 

我的.h文件中(注意事物被注释掉了,因为我尝试不同的事情没有结果):

typedef struct employeeInfo{ 
     char firstname[100]; 
     char lastname[100]; 
     int monthsEmployed; 
}employeeInfo; 

//Struct containing pointers to the next and previous used to make a circular linked list 
typedef struct list{ 
       employeeInfo emp; 
       struct list *next; 
       struct list *previous; 
}employeeList; 

employeeList *insertHeadCircular(employeeList *head, employeeInfo *emp); 
void printInOrder(employeeList head); 
void printReverse(employeeList head); 

employeeList *insertHeadCircular(employeeList *head, employeeInfo *emp) 
{ 
    employeeList *theprevious = head; 
    employeeList *current; 
    employeeList *thenext = head; 
    current = malloc(sizeof(employeeList)); 
    employeeInfo *employee; 

    if(thenext==NULL) 
    { 
     current->next = current; 
     current->previous = current; 
    } 

    else 
    { 
     current->next = thenext; 
     thenext->previous = current; 

     while(theprevious->next != thenext) 
     { 
      theprevious = theprevious->next; 
     } 
     current->previous = theprevious; 
     theprevious->next = current; 
    } 

    current->emp = (employeeInfo *)malloc(sizeof(employeeInfo)); 
    employee = current->emp; 
    employee = malloc(sizeof(employeeInfo)); 
    strcpy(employee->firstname, emp->firstname); 
    strcpy(employee->lastname, emp->lastname); 
    employee->monthsEmployed = emp->monthsEmployed; 

    /* 
    employeeList *newcell, *first = head; 

    if(head == NULL) 
    { 
     newcell = (struct list *)malloc(sizeof(struct list)); 
     strcpy(newcell->firstname, emp->firstname); 
     strcpy(newcell->lastname, emp->lastname); 
     newcell->monthsEmployed = emp->monthsEmployed; 
     return newcell; 
    } 

    while(head->next != first) 
    { 
     head = head->next; 
    } 

    newcell = (struct list *)malloc(sizeof(struct list)); 
    head->next = newcell; 
    strcpy(newcell->firstname, emp->firstname); 
    strcpy(newcell->lastname, emp->lastname); 
    newcell->monthsEmployed = emp->monthsEmployed; 
    newcell->next = first; 
    */ 
return current; 
} 


void printInOrder(employeeList head) 
{ 
    /*employeeInfo *first = head; 

    if (head == NULL) 
     { 
     printf("The circularly linked list is empty!\n"); 
     return; 
     } 

     do 
     { 

     printf("%s %s %d\n", emp.firstname, emp.lastname, head.monthsEmployed); 

     head = head->next; 
     } while(head != first); 
*/ 
    /*employeeInfo current = head; 
    employeeInfo start = head; 
    int loop = 0; 
    printf("--------------\n"); 
    while(current != start || loop==0) 
    { 
    loop++; 
    printf("Employee: %s %s\nMonths Employed: %d", current->firstname, current->lastname, current->monthsEmployed); 
    printf("--------------\n"); 
    current=current->next; 
    }*/ 
} 

void printReverse(employeeList head) 
{/* 
    employeeList current = head 
    employeeInfo start = head 
    int theloop=0; 
    printf("--------------\n"); 
    while(current! = start || loop==0) 
    { 
    loop++; 
    printf("Employee: %s %s\nMonths Employed: %d", current->firstname, current->lastname, current->monthsEmployed); 
    printf("--------------\n"); 
    current=current->previous; 
    }*/ 
} 

编辑好的程序

错误:

file.c: In function âmainâ: 
file.c:37: error: incompatible type for argument 2 of âinsertHeadCircularâ 

的.C:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include "file.h" 

int main (int argc, char **argv) 
{ 

char firstname[100]; 
char lastname[100]; 
int monthsEmployed; 

FILE *fptr; 
fptr = fopen(argv[1], "r"); 

if (fptr == NULL) 
    printf ("Incorrect file reading!"); 

if (argc != 2) 
    printf ("Incorrect number of arguments!"); 


employeeInfo *insert; 
insert = malloc(sizeof(employeeInfo)); 
employeeList *head; 
head = NULL; 


while(!feof(fptr)) 
{ 
    fscanf (fptr, "%100s %100s %d", firstname, lastname, &monthsEmployed); 

    strcpy(insert->firstname, firstname); 
    strcpy(insert->lastname, lastname); 
    insert->monthsEmployed = monthsEmployed; 

    head = insertHeadCircular(head, insert); 
} 

printInOrder(head); // display the linked list 
printReverse(head); // display the linked list in reverse 
} 

的.H:

typedef struct employeeInfo{ 
    char firstname[100]; 
    char lastname[100]; 
    int monthsEmployed; 
}employeeInfo; 

typedef struct list{ 
    employeeInfo emp; 
    struct list *next; 
    struct list *previous; 
}employeeList; 
    typedef employeeList *listnode; 

employeeList *insertHeadCircular(employeeList *head, employeeInfo emp); 
void printInOrder(employeeList *head); 
void printReverse(employeeList *head); 



employeeList *insertHeadCircular(employeeList *head, employeeInfo emp) 
{ 
    listnode newPtr; 
    listnode firstPtr; 
    listnode tempPtr; 

    newPtr = (employeeList *)malloc(sizeof(employeeList)); 

    strcpy(newPtr->emp.firstname, emp.firstname); 
    strcpy(newPtr->emp.lastname, emp.lastname); 
    newPtr->emp.monthsEmployed = emp.monthsEmployed; 

    if(head == NULL) 
    { 
     newPtr->next = newPtr; 
     newPtr->previous = newPtr; 
     head = newPtr; 
     firstPtr = newPtr; 

    } 
    else 
    { 
     tempPtr = firstPtr; 
     newPtr->next = tempPtr; 
     tempPtr->previous = newPtr; 

     newPtr->previous = head; 
     head->next = newPtr; 
     firstPtr = newPtr; 
    } 
    return head; 
} 
void printInOrder(employeeList *head) 
{ 
     listnode currentPtr = head; 
     do 
    { 
      printf("%s %s %d\n",currentPtr->emp.firstname, currentPtr->emp.lastname, currentPtr->emp.monthsEmployed); 
     currentPtr= currentPtr->previous; 
    } 
    while(currentPtr !=head); 
} 
void printReverse(employeeList *head) 
{ 
     listnode currentPtr = head->next; 
     do   
    { 
     printf("%s %s %d\n",currentPtr->emp.firstname, currentPtr->emp.lastname, currentPtr->emp.monthsEmployed); 
     currentPtr = currentPtr->next; 
    } 
    while(currentPtr != head->next);   
} 
+3

你的代码在你的.h文件中? –

+0

他可能正在解决一个实验任务.. :)并在h文件中添加了代码:) – duedl0r

+0

@Flunkie:homework tag?代码中发生错误的行在哪里? – duedl0r

回答

1

在你insertHeadCircular()功能,你对待的employeeListemp成员,就好像是一个employeeInfo *。例如,你声明:

employeeInfo *employee; 

但后来做到这一点:

employee = current->emp; 

但是,你employeeList类型包含的employeeInfo,不是指针的实例之一:

typedef struct employeeInfo{ 
     char firstname[100]; 
     char lastname[100]; 
     int monthsEmployed; 
}employeeInfo; 
/* ... */ 
typedef struct list{ 
       employeeInfo emp; /* see? Not a pointer. */ 
       struct list *next; 
       struct list *previous; 
}employeeList; 

所以基本上你需要纠正你的代码,这样你就可以停止为一个指针分配一个结构,而是将的地址分配给指针的结构。

最有可能你的printInOrder()printReverse()功能应采取employeeList *参数,而不是那些employeeList,以及...你应该检查你使用它们,以确保你不要混淆这两个任意位置的代码。

在头文件以外的地方定义函数也是一个好主意,例如在单独的.c源文件中。头文件应该只包含其他源文件可能需要的函数原型,宏和其他声明;您不需要那里的函数体,因为链接程序可以在从其他源创建的目标文件中找到它们。在这样的头文件中定义函数会在头文件由多个文件编辑时导致无穷无尽的头痛。


你与你的更新代码得到的错误,file.c:37: error: incompatible type for argument 2 of âinsertHeadCircularâ是指出你传递给insertHeadCircular()参数的类型是不是你在其声明中给出的类型 - 这是事实。你已经声明和定义函数采取employeeInfo作为第二个参数:

employeeList *insertHeadCircular(employeeList *head, employeeInfo emp) 

...但在main()你传递一个指针来代替:

employeeInfo *insert; 
... 
    head = insertHeadCircular(head, insert); 

所以你需要改变一个或另一个。从main调用函数时取消引用insert,或者改变insertHeadCircular()以取代指针(并相应地更新主体)。后者可能更好,因为它可以避免在调用函数时将整个结构复制到堆栈中。

一些其他的事情要指出:

你真的应该检查从scanf()在循环中main()回报。它会让你知道是否所有的字段都是实际读取的;现在,如果它们不是,你的程序就会继续处理已有变量的任何垃圾(例如可能在前面的迭代中读取的任何垃圾)。检查其他返回值(如从malloc()返回)也是一个好主意,但在这种情况下,scanf()回报尤其重要。

你也没有在你的程序结束时免费获得insert;当你的程序退出时,操作系统将(几乎可以肯定)清理它,但是当你完成它时,你可以自己做这个练习。不过,您使用它的方式并不是真的需要动态分配它;你可以刚刚宣布一个employeeInfo并采取了其地址(无论以任何方式,但是)。

+0

好吧,我根据您的建议对我的.h文件进行了大量编辑,剩余的错误仅存在于.c文件中,我致电我仍然得到一个错误insertHeadCircular ..'file.c :: error:不兼容类型的参数2âinsertHeadCircularâ'..so ..我在哪里调用它,'head = insertHeadCircular(head,insert); '这是说我从文件中读取的结构是不正确的类型?什么是更好的方法? – Flunkie

+0

@Flunkie您最初发布的代码不应该有特定的错误...检查您的你在头文件中是否仍然有'insertHeadCircular()'的原型(但没有定义)?原型或定义的参数列表中是否存在'*'? – Dmitri

+0

是的,我没有更改'.c',只是'.h'。我拥有的原型是一样的,'employeeList * insertHeadCircul ar(employeeList * head,employeeInfo emp);' – Flunkie