1

My code is raising this error:

supportCrew.cpp:(.text+0x15): undefined reference to `sportsPsychologist::sportsPsychologist()'
supportCrew.cpp:(.text+0x25): undefined reference to `Physiotherapist::Physiotherapist()'
supportCrew.cpp:(.text+0x35): undefined reference to `trainer::trainer()'
supportCrew.cpp:(.text+0x47): undefined reference to `trainer::trainer()'

The two relevent classes/cpp files are supportCrew and Person

Person.cpp

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <chrono>
#include <random>
#include <algorithm>

Person::Person(std::string pName, int pAge, int pExperience) {
    name = pName;
    age = pAge;
    experience = pExperience; //years of experience
};
   

Athlete::Athlete(std::string name, int age, int experience, std::string gender, double height, double weight)
{
    gender = gender;
    height = height;
    weight = weight;
};

Physiotherapist::Physiotherapist(std::string name, int age, int experience, int recovery, int readiness) {
    readinessScore = readiness;
    recoveryScore = recovery;
};

sportsPsychologist::sportsPsychologist(std::string name, int age, int experience, int pressure, int injury, int successFail) {

pressurescore = pressure;
injuryscore = injury;
successfailscore = successFail;

};

trainer::trainer(std::string name, int age, int experience,std::string specialization, int performance, int consistency) {

specialization = specialization;
performanceScore = performance;
consistencyScore = consistency;

};

Person.h


#ifndef PERSON
#define PERSON

class Person {

std::string name;
int experience; //in years
int age;

public:
Person(std::string pName, int pAge, int pExperience);
Person();

virtual ~Person(){};
std::string getName() {return randomName();};
int getAge(){return randomAge();};
int getExperience(){return randomExperience();};
void printData(std::string, int, int);

std::string randomName();
int randomAge();
int randomExperience();

};

class Athlete: public Person{

private: 
std::string gender;
double height;
double weight;

public:
Athlete(std::string pName, int pAge, int pExperience, std::string gender, double height, double weight);
Athlete();
virtual ~Athlete(){};
std::string &getGender(){return gender;};
std:: string randomGender();
double randomHeight();
double randomWeight();
};


class Physiotherapist: public Person{
private:
    int recoveryScore;
    int readinessScore;

public:
Physiotherapist(std::string pName, int pAge, int pExperience, int recoveryScore, int readinessScore); 
Physiotherapist();
int &getscoreRecovery(){return recoveryScore;};
int &getscoreReadiness(){return readinessScore;};
};

class sportsPsychologist: public Person {
private:
    int pressurescore;
    int injuryscore;
    int successfailscore;

public: 
sportsPsychologist(std::string pName, int pAge, int pExperience, int pressureScore, int injuryScore, int successfailScore); 
sportsPsychologist();
int &getscorePressure(){return pressurescore;};
int &getscoreInjury(){return injuryscore;};
int &getscoreSuccues_Fail(){return successfailscore;};    
};

class teamManager: public Person {

public:
teamManager(std::string pName, int pAge, int pExperience); 
teamManager(); 

};

class trainer: public Person {
private:
    std::string specialization; 
    int performanceScore;
    int consistencyScore;

public: 
trainer(std::string pName, int pAge, int pExperience, std::string specialization, int performanceScore, int cocsistencyScore);
trainer();
std::string &getSpecialization(){return specialization;};
int &getscorePeformance(){return performanceScore;};
int &getscoreConsistency(){return consistencyScore;};
};


#endif 

supportCrew.h

#ifndef SUPPORTCREW
#define SUPPORTCREW
#include "Person.h"

class supportCrew {

private:
sportsPsychologist Psychologist;
Physiotherapist physio;
trainer trainer1;
trainer trainer2;

public:
supportCrew(sportsPsychologist,Physiotherapist,trainer,trainer);
supportCrew();
};
#endif

supportCrew.cpp

#include <iostream>
#include <sstream> 
#include <fstream>

#include "compDay.h"
#include "Competion.h"
#include "Events.h"
#include "Location.h"
#include "Octathlon.h"
#include "Person.h"
#include "supportCrew.h"
#include "theTeam.h"
#include "weatherSystem.h"

supportCrew::supportCrew() {
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
davidlingg2000
  • 107
  • 1
  • 5
  • 1
    `specialization = specialization`??? – goodvibration Oct 25 '20 at 05:51
  • 1
    It seems it's happening for the usual reason. You have declared (for instance) `Physiotherapist::Physiotherapist()` but have not defined it, at least not in the code presented here. I'm puzzled as to why you're puzzled, this seems straightforward. Do you think you've defined the missing symbols somewhere? If so where? – john Oct 25 '20 at 05:53
  • @john have I not defined them in my cpp file? – davidlingg2000 Oct 25 '20 at 05:56
  • @davidlingg2000 No. I see `Physiotherapist::Physiotherapist(std::string name, int age, int experience, int recovery, int readiness)` I don't see `Physiotherapist::Physiotherapist()` You've decalred two constructors but defined only one. However I don't think you need the second constructor, that derives from another misunderstanding. See my answer. – john Oct 25 '20 at 06:00
  • Keep an eye on where you use local variables and where member variables and use `this` as appropriate. For example `gender = gender; height = height; weight = weight;` in `Athlete::Athlete(...)` - none of these assignments will have any effect. – Lukas-T Oct 25 '20 at 06:09

1 Answers1

0

It seem the skill you are missing is how to call base class constructors. So

Physiotherapist::Physiotherapist(std::string name, int age, int experience, int recovery, int readiness) {
    readinessScore = readiness;
    recoveryScore = recovery;
};

should really be

Physiotherapist::Physiotherapist(std::string name, int age, int experience, int recovery, int readiness)
    : Person(name, age, experience) {
    readinessScore = readiness;
    recoveryScore = recovery;
}

If you do that then the Person::Person() constructor isn't required because you are explicitly calling the Person::Person(std::string pName, int pAge, int pExperience); constructor.

Of course you should be using initialization lists everywhere. So the above code is even better written like this

Physiotherapist::Physiotherapist(std::string name, int age, int experience, int recovery, int readiness) 
     : Person(name, age, experience)
     , readinessScore(readiness)
     , recoveryScore(recovery) {
}

If you don't call the base class construcor from an initialization list, then the default base class constructor will be called implicitly. I guess this is why you've decalred all these default constructors. But if you code it like I show you above then you (probably) don't need all these default constructors.

john
  • 85,011
  • 4
  • 57
  • 81
  • Aaaaah okay I see what you mean thankyou! I am getting an undefined reference to Person::Person() after fixing all the others. Do I just delete the definition in the person.cpp file? – davidlingg2000 Oct 25 '20 at 06:06
  • @davidlingg2000 Yes you should, but if that results in a compilation error then you need to look where it is being used and see if you really do need it or not. – john Oct 25 '20 at 06:07