我不明白编译器错误

I don't understand the compiler errors

本文关键字:错误 编译器 明白      更新时间:2023-10-16

这是一个家庭作业,所以我不希望您完全编写缺失的代码。但我需要一个相当有力的推动,因为我是新来的,需要熟悉我正在做的事情。

这是adddetailsblablabfunction()中使用的格式块

#define REPLNEFORMT3 "       %-7s%7f%4fn" 

第51行是函数

的原型
51 void AddDetailToAccumulators(float *totpayrate, *float p);/

第85行是在主模块中调用AddDetailToAccumulators()函数

 85 AddDetailToAccumulators(float *totpayrate, *float p);
171 void AddDetailToAccumulators(float *totpayrate, float *p)//3.6
172 {
173 totpayrate = p + totpayrate;
174 }
175 void PrintSummaryReport(float totpayrate, FILE * reportfile)/*, float  totreg, float *totovt, float totg, float totfed,
176 float totstate, float totssi, float totnet, 
177 int numemps, FILE *reportfile)//3.7*/
178                 
179 {
180 fprintf(stdout,REPLNEFORMT3,totpayrate);
181 fprintf(reportfile,REPLNEFORMT3,totpayrate);
182}

编译错误如下:

g++ -Wall -o "main" "main.cpp" (in directory: /media/dylan07/541C-D0D8)
main.cpp:51:49: error: expected identifier before ‘*’ token
 void AddDetailToAccumulators(float *totpayrate, *float p);//, //float *totp, float reg, float *totreg,
                                                 ^
main.cpp:51:50: error: expected ‘,’ or ‘...’ before ‘float’
 void AddDetailToAccumulators(float *totpayrate, *float p);//, //float *totp, float reg, float *totreg,
                                                  ^
main.cpp: In function ‘int main()’:
main.cpp:85:29: error: expected primary-expression before ‘float’
     AddDetailToAccumulators(float *totpayrate, *float p);
                             ^
main.cpp:85:49: error: expected primary-expression before ‘float’
     AddDetailToAccumulators(float *totpayrate, *float p);
                                                 ^
main.cpp: In function ‘void AddDetailToAccumulators(float*, float*)’:
main.cpp:173:19: error: invalid operands of types ‘float*’ and ‘float*’ to binary ‘operator+’
  totpayrate = p + totpayrate;
                   ^
main.cpp: In function ‘void PrintSummaryReport(float, FILE*)’:
main.cpp:180:40: warning: format ‘%s’ expects argument of type ‘char*’, but argument 3 has type ‘double’ [-Wformat=]
  fprintf(stdout,REPLNEFORMT3,totpayrate);
                                        ^
main.cpp:180:40: warning: format ‘%f’ expects a matching ‘double’ argument [-Wformat=]
main.cpp:180:40: warning: format ‘%f’ expects a matching ‘double’ argument [-Wformat=]
main.cpp:181:44: warning: format ‘%s’ expects argument of type ‘char*’, but argument 3 has type ‘double’ [-Wformat=]
  fprintf(reportfile,REPLNEFORMT3,totpayrate);
                                            ^
main.cpp:181:44: warning: format ‘%f’ expects a matching ‘double’ argument [-Wformat=]
main.cpp:181:44: warning: format ‘%f’ expects a matching ‘double’ argument [-Wformat=]
Compilation failed.

我希望我的格式是好的。:)

编辑:燃烧的灯,我爱你!

好,这就是为什么你会得到错误。

对于第一个和第二个错误,您的*float应该是float *。做*float并不意味着任何编译器,因此产生一个错误。另一方面,做float *,告诉编译器你想要一个指向浮点数的指针,并且是完全有效的。

对于第三和第四个错误,您犯了在函数调用中包含类型的错误。别这样!它会产生一个错误。简单地删除类型,使其看起来像AddDetailToAccumulators(totpayrate, p);,这将修复您的错误,假设topayrate和p是指向主函数中定义的浮点数的指针。

对于第五个错误,您试图将两个指针相加。这行不通!我假设您正在尝试使用指向的值,因此您需要添加解引用操作符(*),使其看起来像:*totpayrate = *p + *totpayrate; .

对于第六个错误和警告,您的格式字符串" %-7s%7f%4fn"告诉fprintf()它应该期望一个字符串参数,然后两个float/double参数能够以指定的格式写入输出流。但是,您继续只给它一个float参数。我不能确切地告诉你如何修复这个问题,因为我不知道格式字符串的意图,或者你应该打印什么。我可以告诉你,你要么需要改变你的格式字符串,所以它只需要一个浮点数而不需要字符串,或者给你的PrintSummaryReport()函数添加更多的参数,这样你就可以给fprintf()你的格式字符串告诉它应该期待什么。

在第51行,编译器告诉您使用了间接操作符(*),但在它之前没有类型声明,因此将其更改为float * p。

在第173行,它告诉你,你没有给字符串REPLNEFORMT3足够的格式参数,它期望3个,但你只给了它一个。