-1

Hello I am working on a project for school and the compiler in the terminal seems to be giving me the error 'undefined reference to my class. Does it have something to do with ctors and default ctors? Any suggestions on how to fix this? Thank you!

#include <iostream>
using namespace std;

class Sports{

public:
    Sports();
    Sports(int players, int medals = 0){
        m_players = players;
        m_medals = medals;
    }

    int getPlayers(){return m_players;}
    bool getMedals(){return m_medals;}

friend bool compare(const Sports & lhs, const Sports & rhs){
    return(lhs.m_players == lhs.m_players && rhs.m_medals == rhs.m_medals);
}

protected: 
    int m_players;
//private: 
    int m_medals;
};

class Tennis : public Sports{

public: 
    Tennis(bool experience){
        m_experience = experience;
    }
    void RemovePlayers(){m_players--;}
    void AddMedals(){m_medals++;}
private:
    bool m_experience;

};

int main(){

Tennis t1;


return 0;
}
  • There are a few things amiss in your question: Firstly, what is the [mcve] that demonstrates the issue. Reduce your code until you have the bare minimum with the same, unexpected error. Secondly, what is the actual error you get? You should quote the whole error message, see also [ask]. As a new user, also make sure you take the [tour]. – Ulrich Eckhardt Mar 11 '20 at 06:10
  • Presumably you need to implement the default constructor of `Sports` – Alan Birtles Mar 11 '20 at 07:25

1 Answers1

0

The only constructor for Tennis takes a bool. But you try to construct a Tennis named t1 without passing a bool to the constructor.

Also, you say there's a constructor for Sports that takes no parameters. But there isn't one.

You can fix it by adding the code for the constructor of Sports and passing a bool to the constructor of Tennis, like this:

Sports::Sports() : m_players(0), m_medals(0) { ; } 

int main()
{
    Tennis t1 (false);
    return 0;
}
David Schwartz
  • 179,497
  • 17
  • 214
  • 278