gpt4 book ai didi

c - 在头文件中使用 typedef

转载 作者:行者123 更新时间:2023-11-30 15:55:55 24 4
gpt4 key购买 nike

所以我尝试使用 typedef 在头文件中定义数据。具体来说,我需要将下面的头文件代码指向我的主文件。我不知道该怎么做。我收到一条错误消息,指出“(头文件中定义的所有数据类型)均未声明(首先使用此函数。)”任何人都可以指出我正确的方向吗?谢谢!

编辑:CalcTaxes.o 文件只是传递信息的外部文件。它应该对以下代码没有影响。

下面是头文件:

typedef char STR15[15+1];
typedef char STR10[10+1];
typedef struct EmployeeRecord
{
STR15 lastname;
STR10 firstname;
float hours,
payrate,
defr,
gross,
fedtax,
statetax,
ssitax,
net;
} EmployeeRecord;

下面是主文件:

#include <stdio.h>
#include <stdlib.h>
#include "EmployeeRecord.h"
#include "CalcTaxes.o"
#define ADDR(var) &var
#define REPORTCOLUMNHEADINGS1 "Employee Pay Reg Hrs Gross Fed SSI Net\n"
#define REPORTCOLUMNHEADINGS2 "Name Rate OVT Hrs Pay State Defr Pay\n"
#define BARS "======== ===== ======= ======= ====== ====== =======\n\n"
#define REPORTCOLUMN1 "%s, %s\t%8.2f%8.2f%10.2f%8.2f%8.2f%9.2f\n"
#define REPORTCOLUMN2 "%32.2f%18.2f%8.2f\n\n"

extern void CalculateTaxes(float gross, float defr, float *ft, float *st, float *ssit);

int main()
{
EmployeeRecord r;

float reghours,ovthours;
float ft,st,ssit;
float pr,reg,ovt,gp,fedt,stt,sst,def,np;
float avgpr,avgreg,avgovt,avggp,avgfedt,avgstt,avgsst,avgdef,avgnp;
char answer;
int empCount;

FILE *ReportFile;
ReportFile = fopen("report.txt", "w");
fprintf(ReportFile,REPORTCOLUMNHEADINGS1);
fprintf(ReportFile,REPORTCOLUMNHEADINGS2);
fprintf(ReportFile,BARS);
fclose(ReportFile);

empCount = 0;// count initializations
pr = reg = ovt = gp = fedt = stt = def = sst = np = 0;
do
{

printf("Enter employee's name: "); // input section
scanf("%s%s",firstname,lastname);
printf("Enter hourly pay rate: ");
scanf("%f",ADDR(payrate));
printf("Enter deferred amount: ");
scanf("%f",ADDR(defr));
printf("Enter hours worked this pay period: ");
scanf("%f",ADDR(hours));
if (hours > 40)
{
reghours = 40;
ovthours = hours - 40;
}
else {
reghours = hours;
ovthours = 0;
}
gross = (reghours * payrate)+(ovthours * payrate * 1.5);
CalculateTaxes(gross,defr,&ft,&st,&ssit);//Call 3.5 CalculateTaxes
net = gross - ft - st - ssit;
printf(REPORTCOLUMNHEADINGS1);
printf(REPORTCOLUMNHEADINGS2);
printf(BARS);
printf(REPORTCOLUMN1,lastname,firstname,payrate,reghours,gross,ft,ssit,net);
printf(REPORTCOLUMN2,ovthours,st,defr);
ReportFile = fopen("report.txt", "a");
fprintf(ReportFile,REPORTCOLUMN1,lastname,firstname,payrate,reghours,gross,ft,ssit,net);
fprintf(ReportFile,REPORTCOLUMN2,ovthours,st,defr);
fclose(ReportFile);
empCount = empCount + 1;
pr = pr + payrate;
fedt = fedt + ft;
if (hours > 40)
{
reghours = 40;
ovthours = hours - 40;
}
else {
reghours = hours;
ovthours = 0;
}
reg = reg + reghours;
ovt = ovt + ovthours;
gp = gp + gross;
stt = stt + st;
sst = sst + ssit;
np = np + net;
def = def + defr;
avgpr = pr / empCount;
avgfedt = fedt / empCount;
avgreg = reg / empCount;
avgovt = ovt / empCount;
avggp = gp / empCount;
avgstt = stt / empCount;
avgsst = sst / empCount;
avgnp = np / empCount;
avgdef = def / empCount;

fflush(stdin);
printf(" Repeat (Y/N)? : ");
scanf("%c",ADDR(answer));
} while (answer == 'Y' || answer == 'y');
printf("\n"); // print one line for spacing
printf("\nTotals %17.2f%8.2f%10.2f%8.2f%8.2f%9.2f\n",pr,reg,gp,fedt,sst,np);
printf("%24.2f%10.2f%10.2f\n",ovt,stt,def);
printf("\nAverages %17.2f%8.2f%10.2f%8.2f%8.2f%9.2f\n",avgpr,avgreg,avggp,avgfedt,avgstt,avgnp);
printf("%24.2f%10.2f%10.2f\n",avgovt,avgsst,avgdef);
ReportFile = fopen("report.txt", "a");
fprintf(ReportFile,"\nTotals %17.2f%8.2f%10.2f%8.2f%8.2f%9.2f\n",pr,reg,gp,fedt,sst,np);
fprintf(ReportFile,"%32.2f%18.2f%8.2f\n",ovt,stt,def);
fprintf(ReportFile,"\nAverages %15.2f%8.2f%10.2f%8.2f%8.2f%9.2f\n",avgpr,avgreg,avggp,avgfedt,avgsst,avgnp);
fprintf(ReportFile,"%32.2f%18.2f%8.2f\n",avgovt,avgstt,avgdef);
fclose(ReportFile);
fflush(stdin);
getchar();
return 0;

}

最佳答案

您尚未声明名为名字的变量。

您已经声明了一个以名字作为成员的结构类型,尽管我以前从未见过该语法,所以我不确定它是否有效。我见过的所有示例的类型都在每行上用分号处理每个变量。

也许您想改为引用 r.firstname?

关于c - 在头文件中使用 typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11637285/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com