简体   繁体   中英

GCC compiling error conflicting types

I am getting an error from GCC when trying to compile this code.

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

That is the error I am getting if anyone could please help. I am trying to organize a structure array (WITHOUT using qsort) and then sort it again using a different parameter. Here is my code:

#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);
}
  1. Always solve the first error you are getting. This is definitelly not the first error.
  2. At the point where you declare your functions, struct courses doesn't exist yet. That's why you are getting this error.

Either add forward declaration or move the structure definition before the function declarations.

You have to declare structure cources before you are using it. Do like this:

#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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM