Templatized Member Function Of Non-templatized Class

Hello, this is my first time posting on these forums and my first time working with Templates so I do greatly appreciate everyone's time, thank you.

I'm creating a class which has two templitized functions, the purpose of the class if you're wondering is, to help construct a 'packet' of data from multiple variable types into a const char*

Here is the class so we're on the same page:
1
2
3
4
5
6
7
8
9
10
11
//NetPacket.h
struct NetPacket
{
	template <class DATATYPE>
	void AddData(DATATYPE Data);

	template <class DATATYPE>
	void GetData(DATATYPE Data);

	const char *Data;
};

1
2
3
4
5
6
7
8
9
10
11
12
//NetPacket.cpp
template <class DATATYPE>
void NetPacket::AddData(DATATYPE Data)
{

}

template <class DATATYPE>
void NetPacket::GetData(DATATYPE Data)
{

}


The trouble I'm having is that when I try to use AddData or GetData within the program a compiler error comes up:
1
2
3
error LNK2001: unresolved external symbol
"public: void __thiscall NetPacket::AddData<int>(int)"
(??$AddData@H@NetPacket@@QAEXH@Z) main.obj


I do have the function defined, it seems like I need a separate instance defined for each type I could pass but that would defeat the purpose of the template.

Thank you again for your time and help.
You cannot have template definitions in separate files like this - templates have to be completely inline because the compiler needs to know their definitions at compile time, not link time, in order to instantiate them. Remove the .cpp file and keep the .hpp file.
That did the trick thank you!
Topic archived. No new replies allowed.