2013-10-16 96 views
0

嘗試編譯模板類時出現鏈接器錯誤。對於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

Gaar!問題是,在實現文件中,我包含了SaveToXmlStrategy.cpp文件,而不是.h - 直到我在這裏重新讀取我的問題之後,我意識到問題正在盯着我!