コードで何らかのコンパイラ/リンカー エラーが発生しています。おそらくプリプロセッサに関連しています。エラー メッセージには、「x の複数の定義」と表示されます。ここで、x は、lib.c ファイル内の 4 つの関数のいずれかです。私が使用しているコンパイラ/リンカーは、code:blocks でパッケージ化された GNU GCC コンパイラです。
私は #include の順序を変更しようとしましたが、成功しませんでした。これがコンパイラ エラーではなくリンカー エラーであると私が信じるに至ったのは、意図的な構文エラーを犯した場合、コンパイラがそれを検出し、エラーメッセージを表示せずに中止します。
すべてのヘルプ/アドバイス/批判を歓迎します。事前に感謝します!
main.c ファイルは次のとおりです。
#include <stdlib.h>
#include "lib.c"
int main()
{
getGradeAverage();
return EXIT_SUCCESS;
}
そして lib.c:
#include "defs.h"
void getUserName ()
{
printf ("please enter the your name:");
studentRecord sr;
scanf("%40[^\n]%*c",&sr.studentName);
}
void getCourse (index)
{
printf("please enter the name of course 1:");
courseRecord cr1;
scanf("%40[^\n]%*c",&cr1.courseName);
do{
printf("please enter a grade for course 1:");
if ((scanf("%i",&cr1.grade))>-2)
{
printf("the grade you entered is not on the scale. please try again:");
fflush(stdin);
continue;
}
} while(true);
printf("please enter the name of course 2:");
courseRecord cr2;
scanf("%40[^\n]%*c",&cr2.courseName);
do{
printf("please enter a grade for course 1:");
if ((scanf("%i",&cr2.grade))>-2)
{
printf("the grade you entered is not on the scale. please try again:");
fflush(stdin);
continue;
}
} while(true);
}
void GPAPrint ()
{
int GPA;
studentRecord sr;
courseRecord cr1;
courseRecord cr2;
printf("Student name: %s\n",&sr.studentName);
}
void getGradeAverage ()
{
int index=1;
getUserName();
getCourse(index);
GPAPrint();
return (0);
}
#includes と構造体のほとんどが含まれているため、defs.h ファイルもここに関連しています。
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
#define MAX_LENGTH 40
typedef struct courseRecord
{
char courseName [MAX_LENGTH+1];
int grade;
}courseRecord;
typedef struct studentRecord
{
char studentName [MAX_LENGTH+1];
char courseName[2];
}studentRecord;