在头文件中使用typedef

时间:2012-07-24 18:44:08

标签: c 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;

}

1 个答案:

答案 0 :(得分:1)

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

你已经声明了一个以firstname作为成员的结构类型,虽然我之前从未见过这种语法,所以我不确定它是否会起作用。我见过的所有例子都有类型,每行变换一个分号。

也许您想要引用r.firstname?