模板化类的链接器错误



我在尝试编译模板化类时遇到链接器错误。 我对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;
}

实施.cpp:

#pragma once
#include "SaveToXmlStrategy.cpp"
//inside a function body :
ISaveStrategy<IKeyFrame>& keyFrameSaver = SaveToXmlStrategy<IKeyFrame>(xmlWriter.get());

Gaar! 问题是在实现文件中,我包括SaveToXmlStrategy.cpp文件,而不是.h - 直到我在这里重新阅读我的问题,我才意识到问题正盯着我的脸!

相关内容

  • 没有找到相关文章

最新更新