Pattern with OO caused an unresolved issue with external characters

I do not have a .cpp file because I am using a template. But I still met an unresolved issue with external characters. Does anyone know the reason? Thanks so much if you can help me.

template<class T>
class SQLiteHelper
{
public:
    static SQLiteHelper<T>* getInstance(T* factory) 
    {
        if (NULL == m_sInstance)
        {
            m_sInstance = new SQLiteHelper<T>(factory);
        }
        return m_sInstance;
    }
private:
    SQLiteHelper<T>(T* factory) { m_factory = factory; }
private:
    static SQLiteHelper<T>* m_sInstance;
    sqlite3* m_database;
    T* m_factory;
    std::string m_dbPath;
};

and the problem occurs when I called:

AudioItem item;
AudioDBHelper<AudioItem>::getInstance(&item);

Question:

error LNK2001: unresolved external symbol \"private: static class SQLiteHelper<class AudioItem> * SQLiteHelper<class AudioItem>::m_sInstance" (?m_sInstance@?$SQLiteHelper@VAudioItem@@@@0PAV1@A)
+4
source share
1 answer

This is your static variable in your class. You only declare it in your header, but you also need to define it in your cpp file. This means that you will need to include the static member of the template in cpp.

eg. in the cpp file do the following:

template <class T>
SQLiteHelper<T>* SQLiteHelper<T>::m_sInstance;

.cpp ; , , .

+3

Source: https://habr.com/ru/post/1569381/


All Articles