【问题标题】:Creating a vector of class pointers inside another class?在另一个类中创建一个类指针向量?
【发布时间】:2011-12-13 11:13:11
【问题描述】:

我遇到了我创建的两个类的问题。这是一个简单的赛季计划。我创建了一个名为 Season 的类,它正在创建指向 Game 对象的指针向量。编译器抱怨 Game 是一个未声明的标识符,即使我已经定义并测试了它的工作。

为什么Game类不能在Season类中使用或者我怎样才能让它们被使用(可能嵌套在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;
}

【问题讨论】:

  • 如果您将程序的大小减小到仅出现错误,那将会非常有帮助。通过删除不属于问题的所有内容,您可以将程序减少到大约 10 行。这将使我更容易找到错误,并且可能更容易让您找到自己。有关此调试技术的更多信息,请参阅sscce.org

标签: c++ class


【解决方案1】:

编译器从头开始逐行读取您的程序。在您第一次引用 Game 时:

vector<Game*> games

你还没有声明Game

您必须将Game 声明移到Season 之前,或者您必须forward-declare Game

要转发声明Game,请在Session 的定义之前添加此声明:

class Game;

【讨论】:

  • 我不知道我必须为类制作原型。谢谢你告诉我。
【解决方案2】:

当定义Season 时,仍然没有关于类Game 的未来定义的信息。您必须在Season 之前转发声明Game

class Game;

这将允许您在允许不完整类型的上下文中使用它。在 Season 之前定义 Game 可能更有意义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    相关资源
    最近更新 更多