I have a couple link errors with my code and I cant figure out exactly whats causing them. Heres my code:
Engine.h
1 2 3 4 5 6 7 8 9 10 11 12
|
extern enum options{ ATTACK, SPELL, ITEM, DEFEND, RUN, MAX_OPTIONS};
#include <iostream>
#include "menu.h"
#include "battle.h"
class Engine
{
private: std::string e_title;
public: Engine();
};
| |
Engine.cpp
1 2 3 4 5 6
|
#include "engine.h"
Engine::Engine()
{
e_title = "Test Engine";
}
| |
Menu.h
1 2 3 4 5 6 7 8 9 10
|
#pragma once
template <class T>
class Menu
{
private: int m_x, m_y;
T m_max_options;
public: Menu(int x, int y, T max_options);
void displayMenu();
};
| |
Menu.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include "engine.h"
template<class T>
Menu<T>::Menu(int x, int y, T max_options)
{
m_x = 0;
m_y = 0;
m_max_options = max_options;
}
template<class T>
void Menu<T>::displayMenu()
{
cout << "hi" << endl;
}
| |
battle.h
1 2 3 4 5 6 7 8 9 10
|
#pragma once
class Battle
{
private: Menu<options> *b_menu;
public: Battle();
~Battle();
bool NotOver();
void Update();
};
| |
battle.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include "engine.h"
Battle::Battle()
{
b_menu = NULL;
}
Battle::~Battle()
{
if(b_menu){ delete b_menu; b_menu = NULL; }
}
bool Battle::NotOver()
{
//checks would normaly be conducted
//and return false if the battle was
//determined to be over and true if not.
return true;
}
void Battle::Update()
{
if(b_menu == NULL)
{
b_menu = new Menu<options>(0, 0, MAX_OPTIONS);
}
else
{
b_menu->displayMenu();
}
}
| |
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include "engine.h"
int main()
{
Battle *battle;
battle = new Battle();
while(battle->NotOver())
{
battle->Update();
}
return 0;
}
| |
Sorry if the code is kindof vague. The program Ive writen is very large so I eliminated as much as possible just to try an recreate the error I was getting and this is what I was able to reduce it to. The errors my compiler give me are:
1>battle.obj : error LNK2019: unresolved external symbol "public: void __thiscall Menu<enum options>::displayMenu(void)" (?displayMenu@?$Menu@W4options@@@@QAEXXZ) referenced in function "public: void __thiscall Battle::Update(void)" (?Update@Battle@@QAEXXZ)
1>battle.obj : error LNK2019: unresolved external symbol "public: __thiscall Menu<enum options>::Menu<enum options>(int,int,enum options)" (??0?$Menu@W4options@@@@QAE@HHW4options@@@Z) referenced in function "public: void __thiscall Battle::Update(void)" (?Update@Battle@@QAEXXZ)
1>C:\Documents and Settings\Skrug\Desktop\Text Only
Game\Spells\templateTest\Debug\templateTest.exe : fatal error LNK1120: 2 unresolved externals
In the battle.cpp file, if I comment out the following:
line 25: b_menu = new Menu<options>(0, 0, MAX_OPTIONS);
line 29: b_menu->displayMenu();
the errors disappear. So must be something related to my Menu class.
I dont know if im missing some header files I need to include or if
pointers cant be used with templates, or something stupid as usual. Any help would be appreciated. Thankyou.