函数声明错误,“预期的构造函数、析构函数或类型转换之前”令牌”

发布于 2024-11-02 14:19:46 字数 1536 浏览 6 评论 0原文

我在第 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瑾夏年华 2024-11-09 14:19:46

您没有声明 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");

You're not declaring a return type for shapeDetermine. If for example it is supposed to return an int, it should be declared:

int shapeDetermine(char letter);

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:

void shapeDetermine(char shape);

Another update to address the comments:

You need to actually call the function. In your as-posted code for main() you aren't calling shapeDetermine() anywhere. Try changing your main() to read like this:

cout << "Enter the first letter of the shape:"; 
cin>> letter;
shapeDetermine(letter);
system ("pause");
时光清浅 2024-11-09 14:19:46

您需要定义函数的返回值(void?)

you need to define the return value of your function (void?)

空城缀染半城烟沙 2024-11-09 14:19:46

到目前为止,我可以看到您已经声明了函数 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文