I'm getting linker errors when trying to compile a templated class. I'm not too hot on C++ templated programming and the workings of the compiler (MSBuild/VS2012) and am having difficulty in determining what I've done wrong. I'm compiling with /CLR and I get a series of linker errors (LNK2005) when I try to compile my source files, which approximate to:
ISaveStrategy.h:
#pragma once
#pragma unmanaged
template<class T>
class ISaveStrategy
{
public:
enum SaveResult {OK, Error};
virtual SaveResult Save(const T& itemToSave) = 0;
};
SaveToXmlStrategy.h:
#pragma once
#include <gcroot.h>
#include "ISaveStrategy.h"
#pragma unmanaged
namespace System{namespace Xml{ref class XmlWriter;}}
template<class T>
class SaveToXmlStrategy : public ISaveStrategy<T>
{
public:
SaveToXmlStrategy(gcroot<System::Xml::XmlWriter^> writer)
: m_writer(writer)
{}
virtual SaveResult Save(const T& itemToSave);
private:
gcroot<System::Xml::XmlWriter^> m_writer;
};
SaveToXmlStrategy.cpp:
#pragma once
#include "stdafx.h"
#include "SaveToXmlStrategy.h"
#include "IKeyFrame.h"
#include "IKeyFrameTransition.h"
#include "ICueProvider.h"
#pragma managed
using namespace System;
using namespace System::Text;
template class SaveToXmlStrategy<IKeyFrameTransition>;
SaveToXmlStrategy<IKeyFrameTransition>::SaveResult SaveToXmlStrategy<IKeyFrameTransition>::Save(const IKeyFrameTransition& keyFrame)
{
return SaveResult::OK;
}
template class SaveToXmlStrategy<ICueProvider>;
SaveToXmlStrategy<ICueProvider>::SaveResult SaveToXmlStrategy<ICueProvider>::Save(const ICueProvider& keyFrame)
{
return SaveResult::OK;
}
template class SaveToXmlStrategy<IKeyFrame>;
SaveToXmlStrategy<IKeyFrame>::SaveResult SaveToXmlStrategy<IKeyFrame>::Save(const IKeyFrame& keyFrame)
{
SaveResult result = SaveResult::OK;
return result;
}
Implementation.cpp:
#pragma once
#include "SaveToXmlStrategy.cpp"
//inside a function body :
ISaveStrategy<IKeyFrame>& keyFrameSaver = SaveToXmlStrategy<IKeyFrame>(xmlWriter.get());