C ++如何修复错误的函数未定义引用?

问题描述

我的代码正在引发此错误

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()'

两个相关的事件类/ cpp文件是supportCrew和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 recovery,int readiness) {
    readinessscore = readiness;
    recoveryscore = recovery;
};

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

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

};

trainer::trainer(std::string name,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 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 pExperience,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 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 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 pExperience); 
teamManager(); 

};

class trainer: public Person {
private:
    std::string specialization; 
    int performancescore;
    int consistencyscore;

public: 
trainer(std::string pName,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() {
}

解决方法

您似乎缺少的技能是如何调用基类构造函数。所以

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

应该是

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

如果这样做,则不需要Person::Person()构造函数,因为您正在显式调用Person::Person(std::string pName,int pAge,int pExperience);构造函数。

当然,您应该在所有地方都使用初始化列表。因此,上面的代码甚至像这样写得更好

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

如果不从初始化列表中调用基类构造函数,则将默认隐式调用默认基类构造函数。我想这就是为什么您将所有这些默认构造函数都除名了的原因。但是,如果像我在上面显示的那样进行编码,则您(可能)不需要所有这些默认构造函数。