0

I have a class Player and a class Team.

I want to create a vector of Players in constructor of class Team. I have written a method called fillVector witch creates all the players. I have added method fillVector into class Team witch is shown bellow. I do not think class Player is necessary.

When I compile my programm with codeblocks there is the following error:

Team.cpp|9|undefined reference to `Team::fillTeamVector(std::vector >&)'| |error: ld returned 1 exit status|

This is the Team.cpp code :

#include "Team.h"
#include <vector>
#include <iostream>
#include "Player.h"

Team::Team()
{
    vector<Player> team;
    fillTeamVector(team);

}

    void fillTeamVector(vector<Player>& team){

    cout << "How many players are in the team? " <<endl;
    string name;
    int teamSize,x,y,num,target_line;
    cin >> teamSize;

    for (int i=0 ; i<=teamSize ; i++){
        cout << "Give player's name" << endl;
        cin >> name;
        cout << "Give player's number" << endl;
        cin >> num;
        cout << "Give player's x position" << endl;
        cin >> x;
        cout << "Give player's y position" << endl;
        cin >> y;
        cout << "Give player's target line" << endl;
        cin >> target_line;
        Player newPlayer(name,num,x,y,target_line);
        team.push_back(newPlayer);
    }

}

This is the Team.h code :

#ifndef TEAM_H
#define TEAM_H
#include <iostream>
#include <vector>
#include "Player.h"

using namespace std;

class Team
{
    public:
        Team();

        void fillTeamVector(vector<Player>&);

};

#endif // TEAM_H

This is the main.cpp code :

#include <iostream>
#include <stdio.h>
#include "Player.h"
#include "Team.h"

using namespace std;

int main()
{
    Team team;
    return 0;
}
Aris Kantas
  • 375
  • 1
  • 5
  • 15

3 Answers3

2

You have defined "fillTeamVector" as a free function in team.cpp, while it's a member function in the .h. So fillTeamVector method of Team does not exist, but is called, hence the error. In team.cpp, replace this : void fillTeamVector(vector& team)

by this : void Team::fillTeamVector(vector& team)

bisthebis
  • 523
  • 2
  • 10
2

You've declared the function:

void fillTeamVector(vector<Player>& team){ // ...

But you forgot the "class-scope", that because that function is a member-function so you have to add:

void Team::fillTeamVector(vector<Player>& team){ // ...

Exactly what you've done for the constructor (Team::Team()).

BiagioF
  • 9,368
  • 2
  • 26
  • 50
0

You wrote a function called fillTeamVector.

This is not the same thing as a class method called Team::fillTeamVector, which is called by the constructor.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148