开发者

struct declaration syntax error [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly app开发者_JAVA技巧licable, visit the help center. Closed 12 years ago.

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?


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.


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;
}


"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"


Your code compiles perfectly under g++.

But you could try this:

struct stud {

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜