简体   繁体   English

GCC 编译错误类型冲突

[英]GCC compiling error conflicting types

I am getting an error from GCC when trying to compile this code.尝试编译此代码时,我收到来自 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

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.我正在尝试组织一个结构数组(不使用 qsort),然后使用不同的参数再次对其进行排序。 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.在您声明函数时, struct courses尚不存在。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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