2010-08-01 5 views
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 ========== 

도와주십시오. 이 오류를 어떻게 해결할 수 있습니까?

+3

';'을 잊어 버렸습니까? – msw

+0

예 나는 감사를 보았다 –

+4

나는 당신의 모든 질문이 너무 현지화 된 것처럼 생각해야한다고 생각하기 시작했다. –

답변

3

어쩌면 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; 
} 
0

코드는 ++ g에서 완벽하게 컴파일합니다.

하지만 당신이 시도 할 수 :

struct stud { 

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

<으로 컴파일 된 코드는 코드가 표준을 준수하지 않는다는 것을 의미하지는 않습니다. –

10

오류 메시지를 읽어 보시기 바랍니다 :

오류 C2236를 : 예기치 않은 '구조체' '스터드'. ';'을 잊었 니?

struct stud 선언 끝에 세미콜론이 누락되었습니다.

+0

http://blogs.msdn.com/b/oldnewthing/archive/2010/03/24/9983984.aspx –

1

" ';'을 잊어 버렸습니까?" 그것을 모두 말한다.

구조체를 선언 한 후에는 ";"을 넣어야합니다. 또한이 작업을 수행 할 수 있습니다

struct struct_name { 
struct_variables; } new_str; 

이 구조를 만들고 또한 구조체의 형태의 새로운 변수를 만들 것입니다. 대신


"스터드 stud1"를 작성하는

struct stud{ 

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

그리고 당신은 구조를 만든 후 또한, 당신은 단지이 그 구조 형식의 변수를 선언하는 :

그래서, 당신은 이것을 쉽게 할 수 있었다 "struct stud stud1"의 내용