简体   繁体   中英

Undefined reference to static member function of Singleton class

I implement Singleton class for logging. I declare static of logging_instance_ for create only one instance on class name support_service::logging_service . Service_logging call instance of class support_service::logging_service which declear static variable ::logging_instance_ but show an error

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();                                                                                      
 ...                                                                                                                                                                                 
}

One thing I noticed was that you don't have a body for your logging_service or service_logging constructor, could that have been left out from the code posted?

That is all I could figure out from your code unfortunately, if there are some sections missing then please add them so I can take a further look.

you declare the ctor support_service::logging_service<PATH,LEVEL>::logging_service in the header file support_service.hpp (line 14) and even call it for unspecified PATH and LEVEL in the same header file (line 24). However, you fail to provide a definition, so of course you get an undefined-reference error, what else do you excpect?

Solution: define ctor for your class. Make sure that the definition is either visible from header file support_service.hpp, or that it is instantiated for all possible PATH - LEVEL combinations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM