结构声明语法错误

发布于 2024-09-12 08:12:25 字数 1037 浏览 7 评论 0原文

我有以下代码:

#include <iostream>
using namespace std;
struct  stud{

    int roll;
    char name[10];
    double marks;
    }
struct stud stud1={1,"ABC",99.9};
struct stud stud2={2,"xyz",80.0};
int main(){

    cout<<stud1.marks<<"\n"<<endl;
    cout<<stud1.name<<"\n";
    cout<<stud1.roll<<"\n";


     return 0;
}

但有错误:

1>------ Build started: Project: array_structures1, Configuration: Debug Win32 ------
1>Build started 8/1/2010 9:26:47 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\array_structures1.unsuccessfulbuild".
1>ClCompile:
1>  array_structures.cpp
1>c:\users\david\documents\visual studio 2010\projects\array_structures1\array_structures1\array_structures.cpp(9): error C2236: unexpected 'struct' 'stud'. Did you forget a ';'?
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.84
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

请帮忙。我该如何修复这些错误?

I have the following code:

#include <iostream>
using namespace std;
struct  stud{

    int roll;
    char name[10];
    double marks;
    }
struct stud stud1={1,"ABC",99.9};
struct stud stud2={2,"xyz",80.0};
int main(){

    cout<<stud1.marks<<"\n"<<endl;
    cout<<stud1.name<<"\n";
    cout<<stud1.roll<<"\n";


     return 0;
}

But there are errors:

1>------ Build started: Project: array_structures1, Configuration: Debug Win32 ------
1>Build started 8/1/2010 9:26:47 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\array_structures1.unsuccessfulbuild".
1>ClCompile:
1>  array_structures.cpp
1>c:\users\david\documents\visual studio 2010\projects\array_structures1\array_structures1\array_structures.cpp(9): error C2236: unexpected 'struct' 'stud'. Did you forget a ';'?
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.84
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Please help. How can I fix these errors?

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

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

发布评论

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

评论(4

风吹雨成花 2024-09-19 08:12:26

请阅读错误消息:

错误 C2236:意外的“结构”“螺柱”。您忘记了“;”吗?

您缺少 struct Stud 声明末尾的分号。

Please read the error message:

error C2236: unexpected 'struct' 'stud'. Did you forget a ';'?

You are missing the semicolon at the end of the struct stud declaration.

残疾 2024-09-19 08:12:26

我不懂 C++,但也许:

#include <iostream>
using namespace std;
struct  stud{

    int roll;
    char name[10];
    double marks;
    }; // notice the semicolon
struct stud stud1={1,"ABC",99.9};
struct stud stud2={2,"xyz",80.0};
int main(){

    cout<<stud1.marks<<"\n"<<endl;
    cout<<stud1.name<<"\n";
    cout<<stud1.roll<<"\n";


     return 0;
}

I don't know C++, but maybe:

#include <iostream>
using namespace std;
struct  stud{

    int roll;
    char name[10];
    double marks;
    }; // notice the semicolon
struct stud stud1={1,"ABC",99.9};
struct stud stud2={2,"xyz",80.0};
int main(){

    cout<<stud1.marks<<"\n"<<endl;
    cout<<stud1.name<<"\n";
    cout<<stud1.roll<<"\n";


     return 0;
}
倾城花音 2024-09-19 08:12:26

“你是不是忘记了一个‘;’?”说明了一切;

声明结构后,必须添加“;”。
您也可以这样做:

struct struct_name {
 struct_variables; } new_str;

这将创建结构并创建该结构类型的新变量。

因此,您可以轻松地做到这一点:

struct  stud{

    int roll;
    char name[10];
    double marks;
    } stud1={1,"ABC",99.9}, stud2={2,"xyz",80.0};

而且,在创建结构之后,要声明该结构类型的变量,您只需编写


"stud stud1" instead of "struct stud stud1"

"Did you forget a ';'?" Says it all;

After you declare a struct you must put ";".
You can also do this:

struct struct_name {
 struct_variables; } new_str;

This would create the structure and also create a new variable of that struct type.

So, you could easily have done this:

struct  stud{

    int roll;
    char name[10];
    double marks;
    } stud1={1,"ABC",99.9}, stud2={2,"xyz",80.0};

And also, after you create a structure, to declare a variable of that structure type you just have to write


"stud stud1" instead of "struct stud stud1"

梦里兽 2024-09-19 08:12:26

您的代码在 g++ 下完美编译。

但你可以试试这个:

struct stud {

    int roll;
    char name[10];
    double marks;
};

Your code compiles perfectly under g++.

But you could try this:

struct stud {

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