I have an interface:
//Card.h
#include "../Players/Player.h"
class Card {
public:
virtual void applyEncounter(Player& player) const = 0;
virtual void printInfo() const = 0;
virtual ~Card() {}
};
And a class that inherits from it
// Barfight.h
// ...
#include "../Players/Player.h"
#include "Card.h"
class Barfight : public Card {
public:
Barfight() {}
void applyEncounter(Player& player) const override;
void printInfo() const override;
virtual ~Barfight() {};
private:
static const int LOSE_HP = 10;
};
// Barfight.cpp
#include "Card.h"
#include "Barfight.h"
#include "../Players/Player.h"
void Barfight::applyEncounter(Player& player) const override
{
}
void Barfight::printInfo() const override
{
std::cout << "I don't get printed at all" << endl;
}
But when I run in a main() function:
Barfight myBarfightCard;
myBarfightCard.printInfo();
Command Line (from the root directory):
g++ -std=c++11 -Wall -Werror -pedantic-errors -DNDEBUG -g *.cpp -o my_test
This gives me the error:
<...>/test.cpp:145: undefined reference to `Barfight::printInfo() const'
/usr/bin/ld: /tmp/ccNVNgLR.o: in function `Barfight::Barfight()':
<...>/Cards/Barfight.h:14: undefined reference to `vtable for Barfight'
/usr/bin/ld: /tmp/ccNVNgLR.o: in function `Barfight::~Barfight()':
<...>/Cards/Barfight.h:35: undefined reference to `vtable for Barfight'
collect2: error: ld returned 1 exit status
However, if I do this:
// Barfight.h
// ...
void applyEncounter(Player& player) const override {};
void printInfo() const override {};
// ...
I don't get an error, but it doesn't print what is inside of the Barfight.cpp. How do I still do an implementation that works in the .cpp?
Files:
- test.cpp (has main())
- Players
-
- Player.h
-
- Player.cpp
- Cards
-
- Cards.h
-
- Barfight.h
-
- Barfight.cpp