函数声明错误,“预期的构造函数、析构函数或类型转换之前”令牌”
我在第 4 行遇到错误:
expected constructor, destructor, or type conversion before ';' token
在这一点上,我在函数方面非常弱,所以我知道我的函数声明有问题(?)。有人有什么建议吗?预先感谢...
#include <iostream>
using namespace std;
shapeDetermine (char letter);
int main()
{
char letter;
int side, area, base, height; // lengths to be used in calculating area
cout << "Enter the first letter of the shape:";
cin>> letter;
system ("pause");
return 0;
}
添加:
void shapeDetermine (char shape)
{
int side, area, base, height; // lengths to be used in calculating area
if (letter == 's') //determine what shape is used - square
{
cout<< " Enter the length of side of square:";
cin>> side;
area = side * side; // formula for area of square
cout<< " The area of the square is "<< area<< " cm."<<endl;
}
else if (letter =='t') // triangle
{
cout<< " Enter the height of triangle:";
cin>> height;
cout<< " Enter length of base of triangle:"<< endl;
cin>> base;
area = (base * height) / 2; // formula for area of triangle
cout<< " The area of the triangle is "<< area<< " cm."<<endl;
}
else
{
cout<<" Invalid shape entered."<< endl; // for any character other than s||t
}
}
I'm getting an error on line 4:
expected constructor, destructor, or type conversion before ';' token
I'm pretty weak at this point when it comes to functions, so I know that there is something wrong with my function declaration(?). Anyone have any suggestions? Thanks in advance...
#include <iostream>
using namespace std;
shapeDetermine (char letter);
int main()
{
char letter;
int side, area, base, height; // lengths to be used in calculating area
cout << "Enter the first letter of the shape:";
cin>> letter;
system ("pause");
return 0;
}
Added:
void shapeDetermine (char shape)
{
int side, area, base, height; // lengths to be used in calculating area
if (letter == 's') //determine what shape is used - square
{
cout<< " Enter the length of side of square:";
cin>> side;
area = side * side; // formula for area of square
cout<< " The area of the square is "<< area<< " cm."<<endl;
}
else if (letter =='t') // triangle
{
cout<< " Enter the height of triangle:";
cin>> height;
cout<< " Enter length of base of triangle:"<< endl;
cin>> base;
area = (base * height) / 2; // formula for area of triangle
cout<< " The area of the triangle is "<< area<< " cm."<<endl;
}
else
{
cout<<" Invalid shape entered."<< endl; // for any character other than s||t
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有声明
shapeDetermine
的返回类型。例如,如果它应该返回一个 int,则应该声明:更新以响应 OP 发布的新代码:
新代码很好。但是,如果它出现在文件(或另一个文件)中之后
main()
,那么您仍然需要在调用它之前为其声明一个函数原型。鉴于您发布的函数定义,原型将是:另一个更新以解决评论:
您需要实际调用该函数。在您发布的
main()
代码中,您没有在任何地方调用shapeDetermine()
。尝试将您的main()
更改为如下所示:You're not declaring a return type for
shapeDetermine
. If for example it is supposed to return an int, it should be declared:Updating to respond to the new code the OP posted:
That new code is fine. However, if it appears after
main()
in the file (or in another file) then you still need to declare a function prototype for it before you call it. Given the function definition you've posted, the prototype would be:Another update to address the comments:
You need to actually call the function. In your as-posted code for
main()
you aren't callingshapeDetermine()
anywhere. Try changing yourmain()
to read like this:您需要定义函数的返回值(void?)
you need to define the return value of your function (void?)
到目前为止,我可以看到您已经声明了函数 shapeDetermine,但在其声明中您尚未指定返回类型。我认为即使它是无效的,你也必须指定一个。
So far I can see you have declared the function shapeDetermine but in its declaration you haven't specified a return type. I think you must specify one even if it is void.