如何修复 C++ 中的"expected"错误
How to fix "expected" errors in c++
这是一个查找一个人年龄的程序。C++在第 33 行向我显示"预期"错误。错误是 calculate() 的函数定义。 你能帮我修复它吗?我不明白错误是什么。
#include<iostream.h>
#include<conio.h>
struct date
{
int day;
int month;
int year;
};
date birth;
date current;
void main()
{
void calculate(int,int,int,int,int,int);
cout<<"nEnter your date of birth";
cout<<"nDay: ";
cin>>birth.day;
cout<<"nMonth: ";
cin>>birth.month;
cout<<"nYear: ";
cin>>birth.year;
cout<<"nEnter current date";
cout<<"nDay: ";
cin>>current.day;
cout<<"nMonth: ";
cin>>current.month;
cout<<"nYear: ";
cin>>current.year;
calculate (birth.day,birth.month,birth.year,current.day,current.month,current.year);
getch();
}
// Error on line below
void calculate(int birth.day,int birth.month,int birth.year,int current.day,int current.month,int current.year)
{
int monthdays[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(birth.day>current.day)
{
current.day=current.day=monthdays[birth.month-1];
current.month=current.month-1;
}
else if(birth.month>current.month)
{
current.year=current.year-1;
current.month=current.month+12;
}
int calculated_date=current.date-birth.date;
int calculated_month=current.month-birth.month;
int calculated_year=current.year=birth.year;
cout<<"Present age= "<<calculated_date<<calculated_month<<calculated_year;
}
(33,27) 中存在错误
C++不能将参数作为类的成员变量传递。在
void calculate(int birth.day, ...
birth.day
无效。
但是,可以传递整个类,然后使用成员变量。
改变
void calculate(int,int,int,int,int,int);
到
void calculate(date, date);
然后
calculate (birth.day,birth.month,birth.year,current.day,current.month,current.year);
到
calculate(birth, current);
最后
void calculate(int birth.day,int birth.month,int birth.year,int current.day,int current.month,int current.year)
到
void calculate(date birth, date current)
有很多方法可以改进这一点,通过参考传递
void calculate(const date & birth, date current)
(请注意,current
不是引用,因为它将在函数中修改)并清理calculate
中的几个拼写错误
current.day=current.day=monthdays[birth.month-1];
应该是
current.day=current.day+monthdays[birth.month-1];
或
current.day+=monthdays[birth.month-1];
和
int calculated_date=current.date-birth.date;
应该更像
int calculated_day=current.day-birth.day;
编译器将捕获第二个拼写错误,但可能不会捕获第一个拼写错误。我也不相信calculate
中使用的逻辑,但幸运的是,TurboC++ 附带了 Turbo 调试器,这是当时最好的调试器之一,在我看来,它仍然运行良好。
虽然很难在不看到错误的情况下判断错误的含义,并且很难将其行号与给定格式的发布代码相关联,但错误的最可能原因是 '." 不是标识符的有效字符,因此calculate
函数定义中的所有参数名称都是无效标识符。这可能会导致包含单词"预期"(例如"预期的标识符")的错误消息。
如果必须单独传递所有参数名称,请考虑改用"_"或驼峰大小写作为参数名称。但是,您已经声明了这个方便的date
结构进行传递,因此您可以让函数获取参数date birth
和date current
,而不是两个date
实例中的每个成员。
错误可能来自第void calculate(int birth.day,int birth.month,int birth.year,int current.day,int current.month,int current.year)
行
只需将"."替换为"_"或类似包含在其正文中的内容
[编辑]
除了这一点,我鼓励你修改你的函数,只接收参数中的出生日期和当前日期,这是没有用的,提取它们的字段,而这可以由函数本身进行。
警告,因为您将电流修改为,因此您必须按值接收它,而您可以通过常量引用接收出生。当然,您也可以使用局部变量而不是修改当前...
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- 错误消息"expected expression"....有人知道它为什么这么说吗?
- C++中的"expected unqualified-id"错误是什么?
- 如何修复此错误"expected primary expression before 'int'"?
- 为什么在main()之前断言会导致语法错误"error: expected ')' before numeric constant"?
- 错误:创建模板类时"expected unqualified-id before '{' token"
- C++:"Expected '(' for function-style cast or type construction"错误
- C++:将运算符作为参数传递会导致错误"expected an identifier"
- C 和 C++:带有错误"Expected unqualified-id"的数据文件
- 在C++中获取" expected a ; "错误消息
- 外部"C"导致错误"expected '(' before string constant"
- 如何解决"expected constructor, destructor, or type conversion before ‘(’ token"错误?
- 当我尝试在代码中使用宏时,我收到类似"expected identifier"的错误
- 如何修复 C++ 中的"expected"错误
- "[Error] pointer value used where a floating point value was expected" 如何解决此错误?
- 错误 "expected initializer before 'using'" C++
- C++错误"expected initialiser before '[macroname]'"
- 在2D伊辛模型上工作。C++新手。我在第 23 行收到错误"expected unqualified-id before '{' token"
- "expected unqualified-id before 'void'" & "string in class does not name a type" 错误
- 如何编译 MSHTML v9 的.dll。我目前收到 100 多个"MIDL 2035: constant expression expected"错误