c++ - 对 Singleton 类的静态成员函数的 undefined reference

标签 c++ templates singleton metaprogramming

我实现了用于日志记录的 Singleton 类。我声明 logging_instance_ 的静态只在类名上创建一个实例 support_service::logging_service . 类的 Service_logging 调用实例 support_service::logging_service声明静态变量 ::logging_instance_但显示错误

undefined reference to `support_service::logging_service<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int>::logging_service()' collect2: ld returned 1 exit status

support_service.hpp

namespace support_service
{                  
template<typename PATH = std::string , typename LEVEL =  int>

  class logging_service
 {

    public:

     static  boost::shared_ptr<logging_service<PATH,LEVEL> > instance();   
     ....
   private:

     logging_service();
     logging_service(const logging_service<PATH,LEVEL>&);                                                                                                                                       
     static boost::shared_ptr<logging_service<PATH,LEVEL> >  logging_instance_;                                                                                                                 
 };     
}   

template<typename PATH, typename LEVEL>                                                                                                                                                        
    boost::shared_ptr<logging_service<PATH,LEVEL> > logging_service<PATH,LEVEL>::instance()                                                                                                     
     {                                                                                                                                                                                          
       if(logging_instance_.get() == NULL)                                                                                                                                                      
           logging_instance_ = boost::shared_ptr<logging_service<PATH,LEVEL> >(new logging_service<PATH,LEVEL>``());                                                                              
       return logging_instance_;                                                                                                                                                                
     }

service_logging.hpp

using     namespace support_service;            

template<typename PATH, typename  LEVEL>                                                                                                                                                       
boost::shared_ptr<logging_service<PATH,LEVEL> >support_service::logging_service<PATH,LEVEL>::logging_instance_;


namespace service_logging
 {

     template<typename PATH = std::string, typename LEVEL = int> 
     class service_logging
     {   
       public:
       service_logging();
       bool set_logging(PATH  file_path);
     }; 
}



template<typename PATH, typename LEVEL>
bool service_logging<PATH,LEVEL>::set_logging(PATH  file_path)                                                                                                                                   
{
 boost::shared_ptr<logging_service<PATH,LEVEL> > logging_ =  logging_service<PATH,LEVEL>::instance();                                                                                      
 ...                                                                                                                                                                                 
}

最佳答案

我注意到的一件事是您没有用于 logging_service 或 service_logging 构造函数的主体,是否可以从发布的代码中遗漏?

不幸的是,这就是我从您的代码中得出的全部内容,如果缺少某些部分,请添加它们,以便我进一步查看。

关于c++ - 对 Singleton 类的静态成员函数的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9858565/

相关文章:

c++ - 为什么包含字符串文字的初始化程序对初始化 `char` 数组有效?

singleton - 好吧,全局变量被谴责,单例被鄙视,还有什么选择呢?

c++ - 断言不起作用

c++ - gtksourceview 在代码块中编译

C++访问冲突崩溃

C++11,使用 vs typedef,模板化

php docx模板修改

c++ - 编写自定义分配器

iphone - 创建 TCP 套接字连接并在多个 View 中保持连接打开

ruby-on-rails - ruby rails : Passing argument to singleton