Reputation: 109
I don't have the .cpp file cause I am using template. But I still met the unresolved external symbol problem. Do anyone know the reason? Thanks very 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 issue happens when I called:
AudioItem item;
AudioDBHelper<AudioItem>::getInstance(&item);
Issue:
error LNK2001: unresolved external symbol \"private: static class SQLiteHelper<class AudioItem> * SQLiteHelper<class AudioItem>::m_sInstance" (?m_sInstance@?$SQLiteHelper@VAudioItem@@@@0PAV1@A)
Upvotes: 4
Views: 81
Reputation: 3092
It's your static variable in your class. You only declare it in your header but you also need to define it in your cpp file. That means you will have to include your template static member in the cpp.
E.g. in your cpp file do this:
template <class T>
SQLiteHelper<T>* SQLiteHelper<T>::m_sInstance;
You must use a .cpp file for this to work; it doesn't really matter where, but the compiler has to be able to find the definition for your static template instance.
Upvotes: 3