2011-10-23 2 views
0

내가 만든 두 개의 클래스로 문제가 발생했습니다. 간단한 스포츠 시즌 프로그램입니다. 나는 Game 객체에 대한 포인터의 벡터를 만드는 Season이라는 하나의 클래스를 만들었습니다. 컴파일러는 클래스가 정의되어 있고 테스트를 거쳤음에도 알 수없는 식별자 인 Game에 대해 불평하고 있습니다.다른 클래스 안에 클래스 포인터 벡터를 생성 하시겠습니까?

Game 클래스를 Season 클래스에서 사용할 수 없거나 어떻게 사용할 수 있습니까 (시즌의 공용 부분에서 중첩 시키면 좋을지 나쁜지 알 수 없음).

class Season 
{ 
public: 
    Season(); 
    void add_game(int number, string a, int a_score, string b, int b_score); 

private: 
    vector<Game*> games; 
    int game_high_score; 
    string game_high_score_team; 
    int season_high_score; 
    string season_high_score_team; 
    string champion; 
}; 

Season::Season() 
{ 
    int game_high_score = -2; 
    string game_high_score_team = "Unknown"; 
    int season_high_score = -2; 
    string season_high_score_team = "Unknown"; 
    string champion = "Unknown"; 
} 

void Season::add_game(int number, string a, int a_score, string b, int b_score) 
{ 
    Game* temp_game = new Game(number, a, b, a_score, b_score); 
    games.push_back(temp_game); 
} 

string Season::toStr() const 
{ 
    stringstream out; 

    out << "Number of games in the season: " << games.size() << endl 
     << "game_high_score_team: " << game_high_score_team 
     << "\tScore: " << game_high_score_team << endl 
     << "season_high_score: " << season_high_score 
     << "\tScore: " << season_high_score << endl 
     << "champion: " << champion << endl; 

    return out.str(); 
} 

// Game class stores values and has functions for each game of the season 
class Game 
{ 
public: 
    Game(); 
    Game(int number, string a, string b, int a_score, int b_score); 
    string winner(string a, string b, int a_score, int b_score); 
    string toStr() const; 
    string get_team_a() const; 
    string get_team_b() const; 
    int get_team_a_score() const; 
    int get_team_b_score() const; 
    string get_winner() const; 
    int get_top_score() const; 

private: 
    int game; 
    string team_a; 
    string team_b; 
    int team_a_score; 
    int team_b_score; 
    string won; 
    int top_score; 
}; 

Game::Game() 
{ 
    game = -1; 
    team_a = ""; 
    team_b = ""; 
    team_a_score = -1; 
    team_b_score = -1; 
    won = ""; 
    top_score = -1; 
} 

Game::Game(int number, string a, string b, int a_score, int b_score) 
{ 
    game = number; 
    team_a = a; 
    team_b = b; 
    team_a_score = a_score; 
    team_b_score = b_score; 
    won = winner(team_a, team_b, team_a_score, team_b_score); 
} 

string Game::winner(string a, string b, int a_score, int b_score) 
{ 
    if (a_score > b_score) 
    { 
     top_score = a_score; 
     return a; 
    } 
    else if (a_score < b_score) 
    { 
     top_score = b_score; 
     return b; 
    } 
    else 
    { 
     top_score = a_score; 
     return "Tie"; 
    } 
} 

string Game::toStr() const 
{ 
    stringstream out; 

    out << "Game #" << game << endl 
     << "team_a: " << team_a << "\tScore: " << team_a_score << endl 
     << "team_b: " << team_b << "\tScore: " << team_b_score << endl 
     << "Won: " << won << "\t TopScore: " << top_score << endl; 
    return out.str(); 
} 

int main(int argc, char* argv[]) 
{ 
    string file_name; 
    Season sport; 
    file_name = "season.txt" 

    ifstream fin(file_name); 
    if (fin.fail()) 
    { 
     cout << "Could not read file: " << file_name << endl; 
    } 

    if (fin.is_open()) 
    { 
     string temp; 
     getline(fin, temp); 

     int game; 
     string a; 
     string b; 
     int a_score; 
     int b_score; 
     while (!fin.eof()) 
     { 
      fin >> game >> a >> a_score >> b >> b_score; 
      sport.add_game(game, a, b, a_score, b_score); 
     } 

     // close the input stream from the file. 
     fin.close(); 
    } 

    system("pause"); 
    return 0; 
} 
+0

프로그램의 크기를 오류로 줄이면 도움이 될 것입니다. 문제의 일부가 아닌 모든 것을 삭제함으로써 프로그램을 약 10 줄로 줄일 수있었습니다. 이로 인해 오류를 쉽게 찾아 낼 수 있었고 잠재적으로 자신을 쉽게 찾을 수있었습니다. 이 디버깅 기술에 대한 자세한 내용은 http://sscce.org/를 참조하십시오. –

답변

3

컴파일러는 프로그램을 처음부터 줄 단위로 읽습니다. 먼저 Game를 참조하는 시점에서 :

vector<Game*> games 

아직 Game를 선언하지 않았습니다.

당신은 Season에 전에 Game 선언을 이동해야하거나, 당신은 Game을 앞으로 선언해야합니다.

, Game을 전달-선언 이전에 Session의 정의에이 선언을 추가하려면 :

Season이 정의
class Game; 
+0

수업을 프로토 타이핑해야한다는 것을 몰랐습니다. 제게 알려 줘서 고마워요. – LF4

1

, 클래스 Game의 미래의 정의에 대한 정보는 아직 없다. 당신은 선언 전달해야 GameSeason 전에 :

class Game; 

이 불완전한 유형이 허용되는 곳이 컨텍스트에서 사용할 수있게된다. 먼저 GameSeason 앞에 정의하는 것이 더 합리적 일 수 있습니다.

관련 문제