下面的代码会抛出一个错误“未定义的引用'poredi'”。为什么我得到“未定义的引用”错误?
一切都在这个单一的c文件中定义(除了include中的c库)。
'poredi'只是一个函数,我在typedef的下面定义了一个原型,然后我在文件中将它降低。
看一些类似的问题,我可以说它是在Windows 10上使用MinGW C编译器通过CodeBlocks IDE进行编译的,没有任何其他参数用于编译。
我是一个总C诺伯所以任何帮助表示赞赏。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char ime[20];
char prezime[20];
char registracija[10];
int dRod;
int mRod;
int gRod;
char datumIzdavanja[10];
} vozac;
void poredi(vozac *vozaci, int linije);
int nlines(char *datoteka);
void napuni(FILE *ulaz, vozac *vozaci, int lines);
int main(){
int lines = nlines("Registar.in");
FILE *ulaz = fopen("Registar.in", "r");
int i;
FILE *izlaz = fopen("Najmladji.out", "w");
vozac *vozaci = calloc(lines,sizeof(vozac));
napuni(ulaz,vozaci,lines);
fclose(ulaz);
poredi(vozaci,lines);
for (i = 0; i < 5; i++){
printf("\n%s %s (%d.%d.%d)", vozaci[i].ime, vozaci[i].prezime, vozaci[i].dRod, vozaci[i].mRod, vozaci[i].gRod);
}
fclose(izlaz);
return 0;
}
int nlines(char *datoteka){
FILE *ulaz = fopen("Registar.in", "r");
int brojac = 0;
while (!feof(ulaz)){
char ch = fgetc(ulaz);
if (ch == '\n') brojac++;
}
fclose(ulaz);
return brojac;
}
void napuni(FILE *ulaz, vozac *vozaci, int lines){
int i;
char *linija = calloc(70, sizeof(char));
char *token;
char *token2;
char *znak;
char *znak2;
for (i= 0; i < lines; i++){
fgets(linija,70,ulaz);
token = strtok(linija," ");
strcpy(vozaci[i].prezime, token);
znak = strchr(vozaci[i].prezime,',');
*znak = 0;
token = strtok(NULL," ");
strcpy(vozaci[i].ime, token);
znak2 = strchr(vozaci[i].ime, ';');
*znak2 = 0;
token = strtok(NULL, " ");
strcpy(vozaci[i].registracija, token);
token = strtok(NULL, " ");
token2 = strtok(token, ".");
vozaci[i].dRod = atoi(token2);
token2 = strtok(NULL, ".");
vozaci[i].mRod = atoi(token2);
token2 = strtok(NULL, ".");
vozaci[i].gRod = atoi(token2);
token = strtok(NULL, " ");
strcpy(vozaci[i].datumIzdavanja, token);
}
void poredi(vozac *vozaci, int linije){
int sortirano = 1;
vozac buffer;
while (sortirano){
sortirano = 0;
for (i = 0; i < linije; i++){
if (vozaci[i].gRod > vozaci[i+1].gRod){
buffer = vozaci[i];
vozaci[i] = vozaci[i+1];
vozaci[i + 1] = buffer;
sortirano = 1;
}
else if (vozaci[i].gRod == vozaci[i + 1].gRod){
if (vozaci[i].mRod > vozaci[i + 1].mRod){
buffer = vozaci[i];
vozaci[i] = vozaci[i+1];
vozaci[i + 1] = buffer;
sortirano = 1;
}
else if (vozaci[i].mRod == vozaci[i + 1].mRod){
if (vozaci[i].dRod > vozaci[i + 1].dRod){
buffer = vozaci[i];
vozaci[i] = vozaci[i+1];
vozaci[i + 1] = buffer;
sortirano = 1;
}
}
}
}
}
}
}
'poredi'嵌套函数。移动到外面。 – BLUEPIXY