Search code examples
c++multithreadingfriend

Friend class 'has no member named...'


My main frame in a wxWidgets project starts a thread when a button is pushed. I tried to use this example from the documentation of wxThread class, but I get an error compiling it. It says

||=== Build: Debug in WindowsDgpsGUI (compiler: GNU GCC Compiler) ===|
D:\WindowsDgpsGui\NavigationThread.cpp||In destructor 'virtual NavigationThread::~NavigationThread()':|
D:\WindowsDgpsGui\NavigationThread.cpp|82|error: 'class wxFrame' has no member named 'm_navigationThreadCS'|
D:\WindowsDgpsGui\NavigationThread.cpp|84|error: 'class wxFrame' has no member named 'm_navigationThread'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|

Though I can't find what I did wrong... This is the code of my main frame:

class WindowsDgpsGUIFrame: public wxFrame
{

    public:

        WindowsDgpsGUIFrame(wxWindow* parent,wxWindowID id = -1);
        virtual ~WindowsDgpsGUIFrame();

    protected:
        wxCriticalSection m_navigationThreadCS; //protects the m_navigationThread pointer

        NavigationThread* m_navigationThread;
        friend class NavigationThread;
}

This the header of the thread:

class NavigationThread : public wxThread
{

    public:
        /** \name Constructor */
        NavigationThread(wxFrame* parent, navigation_variables usedVariables);

        /** \name Destructor */
        virtual ~NavigationThread();
    private:
        virtual ExitCode Entry();
        wxFrame* m_parent;
}

and this the destructor of the thread where the error occurs:

NavigationThread::~NavigationThread()
{
     wxCriticalSectionLocker enter(m_parent->m_navigationThreadCS);

     m_parent->m_navigationThread = NULL;
    //dtor
}

Does anyone know what I did wrong? Any help is appreciated.


Solution

  • As the error says: the class WindowsDgpsGUIFrame owns a member m_navigationThreadCS. The base class wxThread doesn't.

    You're trying to access the m_navigationThreadCS member variable through a wxThread pointer

    NavigationThread::~NavigationThread()
    {
         wxCriticalSectionLocker enter(m_parent->m_navigationThreadCS);
                                       ^^^^^^^^
    

    since m_parent is of type wxFrame*, hence your error.

    You can either dynamic_cast your m_parent to the derived class that contains it or just change that type to be WindowsDgpsGUIFrame*