I wrote a template class which extends STL list for some use. I declare this class in a header file and define it in a cpp file. However, when I tried to test it in another cpp file, I found Visual Studio not able to link the functions to the cpp file.
I know I can solve this problem by just including the cpp file, but I wonder if there is another way, a better way?
In case you require further information of my code, I give my template class header file "KA_Buffer.h" and main program "main.cpp" below.
KA_Buffer.h:
#pragma once
#include <list>
typedef int KA_Time;
typedef std::list<KA_Time> KA_TimeList;
/**
* This is a length-limited buffer which would only save limited number of
* elements.
*/
template <class T>
class KA_Buffer :
public std::list<T>
{
public:
KA_Buffer(int length) :
m_MaxLength(length), m_Length(0) {};
~KA_Buffer(void) {};
void add(T);
void pop_front();
iterator find(T);
private:
int m_Length;
int m_MaxLength;
};
main.cpp:
#include <iostream>
#include <Windows.h>
#include "KA_Buffer.h"
int main()
{
KA_Buffer<int> buffer(5);
for (int i = 0; i < 10; i++)
{
buffer.add(i);
for (KA_Buffer<int>::iterator iter = buffer.begin();
iter != buffer.end(); iter++)
{
std::cout<<*iter<<" ";
}
std::cout<<std::endl;
}
system("pause");
return 0;
}
The error messages:
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall KA_Buffer::add(int)" (?add@?$KA_Buffer@H@@QAEXH@Z) referenced in function _main