0
所以我想用typedef在頭文件中定義數據。具體來說,我需要將下面的頭文件代碼指向我的主文件。我不知道如何去做這件事。我收到一個錯誤,指出「(在頭文件中定義的所有數據類型)未聲明(首先使用此函數)。」任何人都可以指向正確的方向嗎?謝謝!在頭文件中使用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;
}
如果您添加了完整的錯誤信息,這與您擁有完整標題和.c文件類似,將會有所幫助。 – Tim 2012-07-24 18:48:03
另外,「#include」CalcTaxes.o「」你在那裏做什麼?除非.o文件不是.o文件,否則不能這樣做,並且應該從試圖解析二進制文件的編譯器中拋出可怕的錯誤。 – Tim 2012-07-24 18:49:56
你有循環依賴嗎?你有兩個頭文件每個都試圖相互「包含」嗎?如果是這樣,那可能是你錯誤的原因。 – 2012-07-24 19:05:20