结构声明语法错误
我有以下代码:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请阅读错误消息:
您缺少 struct Stud 声明末尾的分号。
Please read the error message:
You are missing the semicolon at the end of the
struct stud
declaration.我不懂 C++,但也许:
I don't know C++, but maybe:
“你是不是忘记了一个‘;’?”说明了一切;
声明结构后,必须添加“;”。
您也可以这样做:
这将创建结构并创建该结构类型的新变量。
因此,您可以轻松地做到这一点:
而且,在创建结构之后,要声明该结构类型的变量,您只需编写
"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:
This would create the structure and also create a new variable of that struct type.
So, you could easily have done this:
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"
您的代码在 g++ 下完美编译。
但你可以试试这个:
Your code compiles perfectly under g++.
But you could try this: