函数声明错误,"expected constructor, destructor, or type conversion before ';' token"
function declaration error, "expected constructor, destructor, or type conversion before ';' token"
第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
}
}
您没有为shapeDetermine
声明返回类型。例如,如果它应该返回int类型,则应该声明:
int shapeDetermine(char letter);
更新以响应OP发布的新代码:
那个新代码很好。但是,如果在文件(或其他文件)中 main()
之后出现,则仍然需要在调用它之前为它声明一个函数原型。根据你发布的函数定义,原型应该是:
void shapeDetermine(char shape);
另一个更新来处理注释:
您需要实际调用该函数。在你发布的main()
代码中,你没有在任何地方调用shapeDetermine()
。试着把你的main()
改成这样:
cout << "Enter the first letter of the shape:";
cin>> letter;
shapeDetermine(letter);
system ("pause");
你需要定义函数的返回值(void?)
到目前为止,我可以看到您已经声明了函数shapedecide,但在其声明中您没有指定返回类型。我认为你必须指定一个,即使是无效的。
相关文章:
- 如何解决"invalid conversion from 'char' to 'const char*'"
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- Arduino无法编译名为SP:"Expected ')' before '(' token"的变量
- 为什么我的功能在使用 goto 时会给我带来"expected primary-expression before '}' token"?
- "No suitable conversion function from 'std::string' to 'const char *' exists"
- 如何'Invalid conversion'和'no match for operator'更正这些 c++ 错误
- 如何修复此错误"expected primary expression before 'int'"?
- 访问函数变体时"Invalid conversion"错误
- 为什么在main()之前断言会导致语法错误"error: expected ')' before numeric constant"?
- 提升获取文本的区域设置"Conversion failed"
- 使用结构和数组时的"expected primary expression before '.' token"
- C++17 编解码器在将标准::字符串转换为标准::字符串时抛出"bad conversion"
- 如何解决"Expected expression before " = " token"?
- 如何解决"expected constructor, destructor, or type conversion before ‘(’ token"错误?
- 内存集调用位置的"error: expected constructor, destructor, or type conversion before '(' token"
- 模板类编译"expected constructor, destructor, or type conversion before ‘<’ token"错误
- 错误 : "expected constructor, destructor, or type conversion before 'typedef' "
- C++编译错误"expected constructor, destructor, or type conversion before ‘class’"
- C++ 代码中的错误 "expected constructor, destructor, or type conversion before ‘(’ token" 和"no matching fun
- 函数声明错误,"expected constructor, destructor, or type conversion before ';' token"