So I'm trying to create a generic deck of cards that I can populate with any type of card, here's is the code for the Deck class and the Card class:
Deck.h:
#pragma once
#include "Card.h"
class Deck {
private:
Card* m_current_card;
Card* m_first_card;
public:
Deck();
template<class T>
void populate(T*);
void next();
};
Deck.cpp:
#include "Deck.h"
#include <iostream>
Deck::Deck() {
m_first_card = nullptr;
m_current_card = m_first_card;
}
template<class T>
void Deck::populate(T* card) {
int counter = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 13; j++) {
card = new T(i,j);
if (counter > 0) {
m_current_card->next_card(card);
next();
}
else {
m_first_card = card;
m_current_card = m_first_card;
}
counter++;
}
}
}
void Deck::next(){
m_current_card = m_current_card->get_next();
}
Card.h:
#pragma once
class Card{
private:
int m_suit;
int m_face;
Card* m_next_card;
public:
Card(int,int);
void next_card(Card*);
Card* get_next();
};
Card.cpp:
#include "Card.h"
#include <iostream>
Card::Card(int suit,int face){
m_suit = suit;
m_face = face;
m_next_card = nullptr;
}
void Card::next_card(Card* card){
m_next_card = card;
}
Card* Card::get_next(){
return m_next_card;
}
when I try to execute my code in main for example like the following:
#include "Deck.h"
int main()
{
Card* card = nullptr;
Deck thedeck;
thedeck.populate(card);
}
it gives me this error :
>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall Deck::populate<class Card>(class Card *)" (??$populate@VCard@@@Deck@@QAEXPAVCard@@@Z) referenced in function _main
What am I doing wrong here, am I using templates incorrectly?