Possible Duplicate:
Why can templates only be implemented in the header file?
Template class - unresolved external symbol(s)
not a clue why this is producing a linking error. any clues?
datastream.h
#ifndef DATASTREAM_H
#define DATASTREAM_H
#include <vector>
#include <iostream>
class idatastream
{
public:
idatastream();
template<class T>void write(T data);
bool SetPosition(int position);
void print();
private:
int m_position;
std::vector<char> m_buffer;
};
#endif DATASTREAM_H
datastream.cpp
#include "datastream.h"
idatastream::idatastream()
{
m_position = 0;
}
template<class T>
void idatastream::write(T data)
{
char * getPtr = (char *)&data
int index = 0;
if(m_buffer.size() < m_position)
{
resize(m_position);
for(int i = m_position; i < m_position + sizeof(T); i++)
{
m_buffer.push_back(getPtr[index]);
index++;
m_position++;
}
}
else if(m_buffer.size() == m_position)
{
for(int i = m_position; i < m_position + sizeof(T); i++)
{
m_buffer.push_back(getPtr[index]);
index++;
m_position++;
}
}
else
{
for(int i = m_position; i < m_position + sizeof(T); i++)
{
m_buffer[m_position] = getPtr[index];
index++;
m_position++;
}
}
return;
}
bool idatastream::SetPosition(int position)
{
if (position >= 0)
{
m_position = position;
return true;
}
else
{
return false;
}
}
void idatastream::print()
{
for(int i = 0; i < (int) m_buffer.size(); i++)
{
std::cout << m_buffer[i] << " ";
}
std::cout << " \n";
}
main.cpp
#include "datastream.h"
int main()
{
idatastream test;
int meh = 6;
int peh = 23;
int teh = 65;
test.write<int>(meh);
test.write<int>(peh);
test.write<int>(teh);
test.print();
return 0;
}
error:
error LNK2019: unresolved external symbol "public: void __thiscall idatastream::write<int>(int)" (??$write@H@idatastream@@QAEXH@Z) referenced in function _main
have i just made a silly mistake in defining this template function wrong, or is msvc just giving me trouble? Also, if you want to criticize my code here, go ahead, but if it's along the lines of, "wow, that is the shittiest code ever, here use my awesome stuff instead", its likely to get ignored, no matter how right you are.