函数声明错误,"expected constructor, destructor, or type conversion before ';' token"

function declaration error, "expected constructor, destructor, or type conversion before ';' token"

本文关键字:conversion before token type destructor 错误 声明 expected constructor 函数 or      更新时间:2023-10-16

第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,但在其声明中您没有指定返回类型。我认为你必须指定一个,即使是无效的。

相关文章: