C++数据类型错误

发布于 2024-12-25 08:37:10 字数 963 浏览 1 评论 0原文

每当我运行这个时,我都会遇到编译器错误...我很确定这只是我忽略的一些愚蠢的事情,所以我想我会让你们尝试一下。

#include <iostream>
#include <string>

using namespace std;

class hi
{
public:
    string run()
    {
        hi = "Hello.";
        return hi;
    }
private:
    string hi;
}

int main()
{
    bool end = false;
    string in = "";
    string out = "";

    hi hi;

    while(end != true)
    {
        cout << "Input a Command: ";
        cin >> in;
//        if(in == "help")
//        {
//            out = help.run;
//        }
        if(in == "hi")
        {
            out = hi.run;
        }

        cout << out;
        in = "";
    }
    return 0;
}

我不断收到这些错误:

|6|error: new types may not be defined in a return type|
|6|note: (perhaps a semicolon is missing after the definition of 'hi')|
|18|error: two or more data types in declaration of 'main'|
||=== Build finished: 2 errors, 0 warnings ===|

I keep getting a compilier error whenever I run this... I am quite sure it is just something stupid that I am overlooking, so I though I would let you guys try.

#include <iostream>
#include <string>

using namespace std;

class hi
{
public:
    string run()
    {
        hi = "Hello.";
        return hi;
    }
private:
    string hi;
}

int main()
{
    bool end = false;
    string in = "";
    string out = "";

    hi hi;

    while(end != true)
    {
        cout << "Input a Command: ";
        cin >> in;
//        if(in == "help")
//        {
//            out = help.run;
//        }
        if(in == "hi")
        {
            out = hi.run;
        }

        cout << out;
        in = "";
    }
    return 0;
}

I keep getting these errors:

|6|error: new types may not be defined in a return type|
|6|note: (perhaps a semicolon is missing after the definition of 'hi')|
|18|error: two or more data types in declaration of 'main'|
||=== Build finished: 2 errors, 0 warnings ===|

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

笛声青案梦长安 2025-01-01 08:37:10

在程序接近尾声时,您关闭的大括号数量比打开的大括号数量还要多。您需要删除 return 0; 之前的大括号。

此外,您还需要在结束大括号后用分号终止 class hi 的定义括号,位于 main() 定义之前。

Near the end of your program you are closing more curly brackets than you have opened. You need to remove the curly bracket right before return 0;

Also you need to terminate your definition of class hi with a semicolon after the closing curly bracket, right before the definition of main().

幽蝶幻影 2025-01-01 08:37:10

您忘记了类定义后的分号。

You forgot the semi-colon after your class definition.

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