2012-05-23 54 views
0

我想在VS2010中构建别人的代码。它在VS2005中构建得很好,但我需要升级它。模板宏的链接器错误:无法解析的外部符号

他们定义的宏作为在头如下:

#include <boost/scoped_ptr.hpp> 
#include <boost/utility.hpp> 
#include <boost/thread/once.hpp> 

#define DEFINE_SINGLETON(name) boost::scoped_ptr<name> Singleton<name>::_instance 

template<class T> class Singleton : private boost::noncopyable 
{ 
    public: 
     static T& instance() 
     { 
      boost::call_once(init, flag);   
      return *_instance; 
     } 

     static void init() 
     { 
      _instance.reset(new T()); 
     } 

    protected: 
     Singleton() {} 
     ~Singleton() {} 

    private: 
     static boost::scoped_ptr<T> _instance; 
     static boost::once_flag flag; 
}; 

template<class T> boost::once_flag Singleton<T>::flag = BOOST_ONCE_INIT; 

我已经成功地得到现在的代码来构建,但我获得了大量的连接错误的这个宏:

project1.lib(file1.obj) : error LNK2001: unresolved external symbol "private: static class boost::scoped_ptr<class ClassABC> Singleton<class ClassABC>::_instance" ([email protected][email protected]@@@@[email protected]@@@[email protected]@A) 

宏的一个例子是使用(源文件):

#include "singleton.h" 
DEFINE_SINGLETON(ClassABC); 

我很新,来推动和也templat es(对不起),所以我无法理解为什么当VS2005中的所有链接都很好的时候出现这些错误。它值得一提的,是为了做升级,我们也必须提升我们的加速版本,所以这可能是一个因素 - 一些检查已经完成:

  • 头文件包含在源文件
  • 升压目录添加到include目录(下VC++迪尔斯inproperty页)
  • 添加上去的lib目录到链接器 - >常规 - >附加库的依赖

有关信息,我在编译多线程调试DLL Win32的。

我花了大部分时间使用Google搜索无济于事,所以非常感谢任何帮助!

非常感谢:)

+0

你使用'DEFINE_SINGLETON'宏地方? –

+0

对不起,现在包含使用的宏的例子 – namford

回答

0

我没有看到你所用的发布代码的宏的任何地方。

您需要使用它:

//add this line after BOOST_ONCE_INIT or before it, or wherever you see fit. 
template<class T> DEFINE_SINGLETON(T); 

个人而言,我更喜欢这样的:

template<class T> boost::scoped_ptr<T> Singleton<T>::_instance; 
+0

对不起,现在包含使用的宏的例子 – namford

+0

您忘记使用'模板'部分。看我的代码。 – Nawaz

+0

谢谢,但代码是这样的VS2005和宏调用是这样的(没有类型)遍及代码,并在VS2005中工作得很好......为什么VS2010会有所不同? – namford

相关问题