Pointers, Templates, and Header Files

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.
templates have to go in the header file. You can't put the implementation in a cpp file like you can for normal classes.
Guess Ill need to rethink my code then.
Last edited on
Why does Menu have to be a template at all? From the looks of it, and from the name of it, m_max_options
should always be an integral value, which means it can simply be an unsigned int. The type of it will never
change.
Topic archived. No new replies allowed.