promotion image of download ymail app
Promoted

Error with c++ code, templates?

This is how I call the function:

Zombie dan;

room[i][k].commenceFight<Zombie>(dan);

This is how the function is defined in the .cpp file:

template <typename T>

void Room::commenceFight(T mob)

{

mob.enemyDialogue();

cout << mob.calculateDamage();

}

This is how the function is defined in the header file:

template <typename T>

void commenceFight(T);

When I run this code, I get this error.

Severity Code Description Project File Line Suppression State

Error LNK2019 unresolved external symbol "public: void __thiscall Room::commenceFight<class Zombie>(class Zombie)" (??$commenceFight@VZombie@@@Room@@QAEXVZombie@@@Z) referenced in function "public: void __thiscall Dungeon::test(void)" (?test@Dungeon@@QAEXXZ) FinalProject C:\Users\Sevak\source\repos\FinalProject\FinalProject\Dungeon.obj 1

I am new to using templates, so perhaps I am doing something wrong? Any help is appreciated.

2 Answers

Relevance
  • 9 months ago

    Well done.

    Common error!

    Don't forget!

    • Commenter avatarLogin to reply the answers
  • 9 months ago

    I actually solved this problem myself. The problem was that my template function was defined in the .cpp file. I just moved into and defined it in the header file. This fixed the problem.

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.