我在尝试编译模板化类时遇到链接器错误。我对C ++模板化编程和编译器的工作方式(MSBuild / VS2012)并不太热,并且很难确定我做错了什么。我正在使用/ CLR进行编译,当我尝试编译我的源文件时,我得到了一系列链接器错误(LNK2005),这些文件大致为:
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());
答案 0 :(得分:0)
GAAR!问题是在实现文件中我包含了SaveToXmlStrategy.cpp文件,而不是.h - 直到我在这里重新阅读我的问题,我才意识到这个问题正在盯着我! / p>