2011-04-30 6 views
0

구조체의 벡터를 전역 적으로 선언하고 초기화하려고합니다. # include <string><vector> 사람들의구조체의 전역 벡터 사용

Struct creation (in header file vsystem.h) 
    struct var { 
     string name; 
     float value; 
    }; 

Variable (in source file vsystem.cpp) 
    #include <string> 
    #include <vector> 

    vector <var> varList; 

모두 : 내 코드는 다음과 같다. 나는 또한 시도했다

vector <var> varList(); 

그러나 그것은 작동하지 않는다. 내 오류 내 setVar 함수가 에러를 유발한다,

보조 노트에
expected constructor, destructor, or type conversion before '<' token 

입니다 :

Multiple markers at this line 
    - 'string' was not declared in this scope 
    - expected primary-expression before 'float' 
    - initializer expression list treated as compound 
    expression 
    - expected ',' or ';' before '{' token 

코드 :

int setVar(string varName, float value){ 
    // Check to see if varName already exists 
    varExists = false; 
    for (int i=0; i<varList.size(); i++){ 
     if (varList[i].name == varName){ 
      varExists = true; 
      return ERR_VAR_EXISTS; 
     } 
    } 

    // Good! The variable doesn't exist yet. 
    var tempVar(); 
    var.name = varName; 
    var.value = value; 
    varList.push_back(tempVar); 
    return 0; 
} 

도와주세요!

Mac 10.6.7에서 G ++ 컴파일러로 Eclipse Helios Service Release 2를 실행하고 있습니다.

+0

제발, 미리보기 기능을 사용하고 코드를 포맷하십시오. – Potatoswatter

+0

이름으로 값을 찾는 이름/값 쌍을 저장하려고하면'std :: map '이 더 적절할 수 있습니다. –

답변

3

vectorstd 네임 스페이스에 있습니다. 당신은 당신의 선언에 std::vector로 자격을해야합니다

std::vector <var> varList; 

(당신이 정말로 std:: 싫어 경우 또는 당신이하는 사용 선언, using std::vector;를 사용할 수 있습니다.) string에 대한 유사

; std::string으로 규정되어야합니다. C++ 표준 라이브러리의 모든 이름은 (a) 매크로 및 (b) 기존 C 헤더에있는 매크로 (.h으로 끝나는 매크로)를 제외한 std 네임 스페이스에 있습니다.

+1

그리고 그는 3 개를 포함해야합니다 : '#include #include #include "vsystem.h"- 두 가지가 아닙니다. –

+0

도움에 감사드립니다. 방금 네임 스페이스 std를 사용하여 추가했습니다. std :: map을 살펴 보겠습니다. 또한 게시물을 미리 보는 방법은 무엇입니까? @ c-smile : 다른 것들을 고쳤습니다, 고마워요. 당신이 그것을 포함해야한다는 것을 몰랐지만, 그것은 의미가 있습니다. 다른 gazillion 오류에 대한 새로운 스레드를 만들겠습니다 ... – wchargin