2012-11-14 182 views
1

嘗試編譯此代碼時,GCC發生錯誤。GCC編譯錯誤衝突類型

test.c: At top level: 
test.c:35: error: conflicting types for âprintCoursesâ 
test.c:4: error: previous declaration of âprintCoursesâ was here 
test.c:59: error: conflicting types for âidSortâ 
test.c:5: error: previous declaration of âidSortâ was here 
test.c:100: error: conflicting types for âcourseSortâ 
test.c:6: error: previous declaration of âcourseSortâ was here 
test.c:137:2: warning: no newline at end of file 

這是我得到的錯誤,如果任何人都可以請幫助。我正在嘗試組織一個結構數組(不使用qsort),然後使用不同的參數對其進行重新排序。這裏是我的代碼:

#include <stdio.h> 
#include <string.h> 

void printCourses(struct courses *classList, int left, int size); 
void idSort(struct courses *classList, int left, int right); 
void courseSort(struct courses *classList, int left, int right); 

//STRUCTURE 

struct courses 
{ 
char course[50]; 
char courseName[50]; 
int courseID; 
}; 

//MAIN 

int main(void) 
{ 
    int a, b,temp,i; 
    int size = 11; 
    struct courses classList[11]={{"MATH", "Linear Algebra", 3330},{"CSE", "Discrete   
    Structures", 2315},{"CSE","Intermediate Programming",1320},{"IE","Engineering Economics", 3312},{"CSE","Computer Organization", 2312}, 
    {"MATH","Calculus I",1426},{"CSE","Introductory Programming", 1310},{"MATH","Calculus II", 2425},{"IE","Engineering Statistics",3301},{"CSE","Operating Systems", 3320},{"CSE","Data Structures and Algorithms", 2320}}; 

    courseSort(classList, 0, size-1); 
    printCourses(classList, 0, size-5); 
} 

//FUNCTION 1 

void printCourses(struct courses *classList, int left, int size) 
{ 
    int l,r; 
    if(left < size) 
    { 
     l = left; 

     printf("%s\n",classList[left].course); 
     printf("------------------------------------\n"); 
     while(l < size) 
     { 
      printf("%d %s\n",classList[l].courseID,classList[l].courseName); 
      l++; 
     } 

     printCourses(classList,left+6,size+2); 
     printCourses(classList,left+8,size+5); 
    } 

} 


//FUNCTION 2 

void idSort(struct courses *classList, int left, int right) 
{ 

    int pivot, l, r, comp, comp1; 
    struct courses temp; 
    if(left < right) 
    { 
    pivot = left; 
    l = left; 
    r = right; 
    while(l < r) 
{ 

    while(classList[l].courseID <= classList[pivot].courseID && l <= right) 

      l++; 

    while(classList[r].courseID > classList[pivot].courseID && r >= left) 

      r--; 

    if(l < r) 
    { 
     temp = classList[l]; 
     classList[l] = classList[r]; 
     classList[r] = temp; 
    } 
} 


temp = classList[r]; 
classList[r] = classList[pivot]; 
classList[pivot] = temp; 

idSort(classList, left, r-1); 
idSort(classList, r+1, right); 
    } 
} 

//FUNCTION 3 

void courseSort(struct courses *classList, int left, int right) 
{ 
    int pivot, l, r, comp, comp1; 
    struct courses temp; 
    if(left < right) 
    { 
    pivot = left; 
    l = left; 
    r = right; 

    while(l < r) 
    { 

    while(strcmp(classList[l].course, classList[pivot].course) <= 0 && l <= right) 
      l++; 
    while(strcmp(classList[r].course, classList[pivot].course) > 0 && r > left) 
      r--; 

    if (l < r) 
    { 
     temp = classList[l]; 
     classList[l] = classList[r]; 
     classList[r] = temp; 
    } 
} 


temp = classList[r]; 
classList[r] = classList[pivot]; 
classList[pivot] = temp; 

courseSort(classList, left, r-1); 
courseSort(classList, r+1, right); 

} 
idSort(classList,left,right-5); 
idSort(classList,left+6,right-3); 
idSort(classList,left+8,right-1); 
} 
+2

嘗試把結構放在第一位,在使用它的原型之前。 –

回答

3
  1. 總是能夠解決你所得到的第一個錯誤。這是definitelly不是第一個錯誤。
  2. 在聲明你的函數時,struct courses還不存在。這就是你得到這個錯誤的原因。

可以在函數聲明之前添加前向聲明或移動結構定義。

+0

我知道它必須是那樣簡單的東西。我試圖弄清楚自己正在自殺。謝謝! – bardockyo

3

您必須在使用它之前申報結構資源。像這樣做:

#include <stdio.h> 
#include <string.h> 


//STRUCTURE 

struct courses 
{ 
char course[50]; 
char courseName[50]; 
int courseID; 
}; 

void printCourses(struct courses *classList, int left, int size); 
void idSort(struct courses *classList, int left, int right); 
void courseSort(struct courses *classList, int left, int right); 
+0

謝謝!做到了! – bardockyo

+0

然後請將答案標記爲正確。 –