Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm still confused with pointers, maybe you could enlighten me a bit.

I'm having some classes pointing to each other and don't get the way to access them in the right way.

#include <vector>
#include <iostream>

class Player;
class Trainer;
class Team {
    Trainer* trainer_;
    std::vector<Player*> player_;
public:
    std::vector<Player*> player() { return player_; }
    Trainer* trainer() { return trainer_; }

    std::vector<Player*> get_playerlist(){
        return player_;
    };
};

class Player {
public: 

    void setteam_(Team* x){
        team_ = x;
    }
    private:
    Team* team_;
};

class Trainer {
    Team* Team_;
};
int main()
{
    Player player1;
    Team team1;
    std::vector<Player*> playerlist;
    player1.setteam_(&team1);
    playerlist = team1.get_playerlist();
    std::cout << playerlist.size();
    std::cin.get();
    return 0;
}

In the main there is player1 created and assigned to the playerlist of team1, now he should somehow appear there when i get the playerlist. But the output of this code is that the size of the playerlist of team 1 is still 0. What did I do wrong ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
159 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your Team class has no method that actually adds players to the player_ vector. Setting the team_ pointer to the team1 instance is not sufficient.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...