Singleton template problem Topic is solved

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
Post Reply
sethjackson
Super wx Problem Solver
Super wx Problem Solver
Posts: 396
Joined: Wed Oct 05, 2005 1:19 am

Singleton template problem

Post by sethjackson »

Hi. I keep getting link errors with this code... I'm probably doing something dumb. :P Can someone help? Thanks.

singleton.h

Code: Select all

#ifndef SINGLETON_H
#define SINGLETON_H

template<class T> class Singleton
{
    public:

        static T* Get();

        static void Free();

    protected:

        Singleton();
        ~Singleton();

    private:

        static T* m_pInstance;
};

#endif // SINGLETON_H

singleton.cpp

Code: Select all

#include "singleton.h"

template<class T> Singleton<T>::Singleton()
{

}

template<class T> Singleton<T>::~Singleton()
{

}

template<class T>T* Singleton<T>::Get()
{
    if (m_pInstance == 0)
        m_pInstance = new T;

    return m_pInstance;
}

template<class T>void Singleton<T>::Free()
{
    delete m_pInstance;
    m_pInstance = 0;
}

template<class T>T* Singleton<T>::m_pInstance = 0;

editormanager.h

Code: Select all

#ifndef EDITORMANAGER_H
#define EDITORMANAGER_H
....
#include "singleton.h"
..

class EditorManager : public Singleton<EditorManager>, public wxEvtHandler
{
    public:

        ...

    private:

        EditorManager();
        ~EditorManager();

        ...
};

#endif

editormanager.cpp

Code: Select all

#include "editormanager.h"

EditorManager::EditorManager()
{
    // Connect event handlers.

    ConnectEventHandlers();

    // Create a notebook so we can have multiple documents open.

    m_pNotebook = new wxNotebook(Manager::Get()->GetAppWindow(),
                                 wxID_ANY,
                                 wxDefaultPosition,
                                 wxDefaultSize);

    m_pEasyPrinting = new wxHtmlEasyPrinting(_("Printing"), Manager::Get()->GetAppWindow());

    m_pFindReplaceDialog = 0;

    m_pFindReplaceData = new wxFindReplaceData;

    Manager::Get()->GetAppWindow()->PushEventHandler(this);
}

EditorManager::~EditorManager()
{
    if (m_pEasyPrinting)
    {
        delete m_pEasyPrinting;
        m_pEasyPrinting = 0;
    }

    if (m_pFindReplaceData)
    {
        delete m_pFindReplaceData;
        m_pFindReplaceData = 0;
    }

    if (m_pFindReplaceDialog)
    {
        m_pFindReplaceDialog->Destroy();
        m_pFindReplaceDialog = 0;
    }

    for (unsigned int i = 0; i < m_pNotebook->GetPageCount(); ++i)
    {
        Editor* ed = static_cast<Editor* >(m_pNotebook->GetPage(i));

        if (ed->IsModified())
        {
            wxString msg = _("The text in the ");

            msg.append(ed->GetFilename());
            msg.append(_(" file has changed!"
                         "\n\nDo you want to save changes in the "));
            msg.append(ed->GetFilename());
            msg.append(_(" file?"));

            wxMessageDialog dlg(Manager::Get()->GetAppWindow(),
                                msg,
                                _("File changed"),
                                wxICON_QUESTION | wxYES_NO);

            int ret = dlg.ShowModal();

            switch (ret)
            {
                case wxID_YES:
                {
                    ed->Save();
                }
                break;

                case wxID_NO:
                {

                }
                break;

                default:
                    break;
            }
        }
    }

    m_pNotebook->Destroy();

    Manager::Get()->GetAppWindow()->PopEventHandler(false);
}

....
errors
P:\Programming\C++\wxWidgets\Text Editor\editormanager.cpp:4: undefined reference to `Singleton<EditorManager>::Singleton()'
P:\Programming\C++\wxWidgets\Text Editor\editormanager.cpp:22: undefined reference to `Singleton<EditorManager>::~Singleton()'
P:\Programming\C++\wxWidgets\Text Editor\editormanager.cpp:88: undefined reference to `Singleton<EditorManager>::~Singleton()'
P:\Programming\C++\wxWidgets\Text Editor\editormanager.cpp:88: undefined reference to `Singleton<EditorManager>::~Singleton()'
P:\Programming\C++\wxWidgets\Text Editor\editormanager.cpp:88: undefined reference to `Singleton<EditorManager>::~Singleton()'
P:\Programming\C++\wxWidgets\Text Editor\editormanager.cpp:88: undefined reference to `Singleton<EditorManager>::~Singleton()'
objs\Debug\editormanager.o:P:\Programming\C++\wxWidgets\Text Editor\editormanager.cpp:88: more undefined references to `Singleton<EditorManager>::~Singleton()' follow
P:\Programming\C++\wxWidgets\Text Editor\manager.cpp:37: undefined reference to `Singleton<EditorManager>::Free()'
P:\Programming\C++\wxWidgets\Text Editor\manager.cpp:59: undefined reference to `Singleton<EditorManager>::Get()'
eranif
Moderator
Moderator
Posts: 610
Joined: Tue Nov 29, 2005 7:10 pm
Location: Israel

Post by eranif »

Hi,

A quote:

Because templates are compiled when required, this forces a restriction for multi-file projects: the implementation (definition) of a template class or function must be in the same file as its declaration. That means that we cannot separate the interface in a separate header file, and that we must include both interface and implementation in any file that uses the templates.

Move the Singleton.cpp file into the header file, that should fix your problem
Eran
IDE: CodeLite + wxCrafter
OS: All
https://wxcrafter.codelite.org
https://codelite.org
sethjackson
Super wx Problem Solver
Super wx Problem Solver
Posts: 396
Joined: Wed Oct 05, 2005 1:19 am

Post by sethjackson »

Aww..... Ok. Thanks. :D
Post Reply