2011-10-10 66 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