0

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?

trincot
  • 317,000
  • 35
  • 244
  • 286
  • You can move `Deck::populate(T* card)` to the header but why is Deck::populate(T* card) a function template? I don't envision the type being anything different from `Card`. – drescherjm Mar 25 '20 at 20:10
  • what do you mean by moving it to the header? and in this example, I just want to populate with a Card type object, there will be other cases where I want to populate it with a class object that inherits from Card. – Ahmed Alarbi Mar 25 '20 at 20:15
  • Your function template must be defined in the header if you are going to use it from a different c++ source file than `Deck.cpp`. However I question why this is a function template at all. Why not change `template void populate(T*);` to `void populate(Card*);` – drescherjm Mar 25 '20 at 20:17
  • ***there will be other cases where I want to populate it with a class object that inherits from Card.*** You don't need a template function for that. – drescherjm Mar 25 '20 at 20:35
  • how do you suggest doing it? – Ahmed Alarbi Mar 25 '20 at 22:13
  • Since the question is closed I can't post an answer although I thought I already said how to do this several times. – drescherjm Mar 25 '20 at 22:29

0 Answers0