2011-10-23 37 views
0

我與我創建了兩個班運行中的問題。這是一個簡單的賽季節目。我創建了一個名爲Season的類,它創建了一個指向Game對象的指針向量。編譯器抱怨遊戲是,即使我有類中定義並測試了它的工作未聲明的標識符。創建另一個類的內部類指針的載體?

怎麼會遊戲類不能在賽季類使用或如何可以讓他們使用(也許巢它季節的公共部分不知道那會是好或壞)?

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定義之前添加:

class Game; 
+0

我不知道我必須爲這個課程做原型。謝謝你讓我知道。 – LF4

1

Season定義,但仍然沒有關於一類Game的未來定義信息。您可以選擇將申報GameSeason

class Game; 

這將讓你使用它的情況下,其中一個不完整的類型是允許的。它可能會更有意義Season之前定義Game,開始。

相關問題