简体   繁体   English

我不明白编译器错误

[英]I don't understand the compiler errors

This is a homework assignment, so I am not wanting you to completely write the missing code. 这是一项家庭作业,所以我不希望您完全编写缺少的代码。 But I need a pretty hard push because I am new and need to get familiar with what I am doing. 但是我需要大力推动,因为我是新来的,需要熟悉我在做什么。

This is a formatpiece used in AddDetailsblablablafunction() 这是AddDetailsblablablafunction()中使用的格式

#define REPLNEFORMT3 "       %-7s%7f%4f\n" 

Line 51 is a prototype of function 第51行是功能原型

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

Line 85 is In the main mopdule and calls the AddDetailToAccumulators() function 第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}

The compiler errors are listed as follows: 编译器错误列出如下:

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.

I hope my formatting is good. 我希望我的格式很好。 :) :)

EDIT: BurningLights, I love you! 编辑:BurningLights,我爱你!

Okay, so here's why you're getting your errors. 好的,这就是为什么您遇到错误的原因。

For the first and second errors, your *float should be float * . 对于第一个和第二个错误,您的*float应该是float * Doing *float doesn't mean anything to the compiler, and so generates an error. *float对编译器没有任何意义,因此会产生错误。 Doing float * on the other hand, tells the compiler your want a pointer to a float, and is perfectly valid. 另一方面,执行float *告诉编译器您想要一个指向float的指针,并且完全有效。

For the third and fourth errors, you made the mistake of including the types in your function call. 对于第三个和第四个错误,您犯了在函数调用中包括类型的错误。 Don't do this! 不要这样! It generates an error. 它产生一个错误。 Simply remove the types so it looks like AddDetailToAccumulators(totpayrate, p); 只需删除类型,使其看起来像AddDetailToAccumulators(totpayrate, p); and that will fix your errors, assuming totpayrate and p are pointers to floats defined in your main function. 假设totpayrate和p是指向主函数中定义的浮点的指针,那么这将解决您的错误。

For the fifth error, you're trying to add two pointers together. 对于第五个错误,您尝试将两个指针加在一起。 That doesn't work! 那不行! I assume you're trying to use the values that are pointed to, so you need to add the dereference operator (*) to make it look like: *totpayrate = *p + *totpayrate; 我假设您正在尝试使用所指向的值,因此您需要添加解引用运算符(*)使其看起来像: *totpayrate = *p + *totpayrate; .

For the sixth error and the warnings, your format string " %-7s%7f%4f\\n" tells fprintf() that it should expect a string argument, and then two float/double arguments to be able to write out to the output stream in the specified format. 对于第六个错误和警告,您的格式字符串" %-7s%7f%4f\\n"告诉fprintf()应该包含一个字符串参数,然后两个浮点/双精度参数能够写出到输出中以指定的格式流。 However, you proceed to only give it a single float argument. 但是,您将继续只给它一个float参数。 I can't exactly tell you how to fix this one, since I don't know the intent of the format string, or what you're supposed to be printing. 我无法确切地告诉您如何解决此问题,因为我不知道格式字符串的意图或您应该打印的内容。 I can tell you that you'll either need to change your format string so it only needs a single float and no string, or add more parameters to your PrintSummaryReport() function so you can give fprintf() what your format string tells it that it should expect. 我可以告诉您,您要么需要更改格式字符串,以使其只需要一个浮点数就不需要字符串,或者可以向PrintSummaryReport()函数添加更多参数,以便为fprintf()格式字符串告诉您的内容,它应该期望。

On line 51 the compiler is telling you that the indirection operator(*) is used but you don't have a type declaration before it, so change to float * p. 在第51行,编译器告诉您使用了间接操作符(*),但您之前没有类型声明,因此将其更改为float * p。

On line 173 it's telling you that you didn't give enough format arguments for the string REPLNEFORMT3, it expects 3 but you only gave it one. 在第173行,它告诉您您没有为字符串REPLNEFORMT3提供足够的格式参数,它期望3,但您只给了它一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM