朋友 class 'has no member named...'

Friend class 'has no member named...'

我在 wxWidgets 项目中的主框架在按下按钮时启动一个线程。我尝试使用 wxThread class 文档中的 this example,但编译时出现错误。它说

||=== 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)) ===|

虽然我找不到我做错了什么...... 这是我的主框架的代码:

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;
}

这是线程的header:

class NavigationThread : public wxThread
{

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

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

这是发生错误的线程的析构函数:

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

     m_parent->m_navigationThread = NULL;
    //dtor
}

有谁知道我做错了什么?感谢任何帮助。

如错误所述:class WindowsDgpsGUIFrame 拥有一个成员 m_navigationThreadCS。基础 class wxThread 没有。

您正在尝试通过 wxThread 指针访问 m_navigationThreadCS 成员变量

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

因为 m_parentwxFrame* 类型,因此你的错误。

您可以 dynamic_cast 您的 m_parent 到包含它的派生 class 或者只是将该类型更改为 WindowsDgpsGUIFrame*

因此,总结一下之前答案的评论,它是这样工作的:

NavigationThread.h:

class WindowsDgpsGUIFrame; // Forward declare wxFrame class

/** \brief Class for working thread */
class NavigationThread : public wxThread
{

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

        /** \name Destructor */
        virtual ~NavigationThread();
}

NavigationThread.cpp

#include "WindowsDgpsGUIMain.h" // Include main class where frame is placed

NavigationThread::NavigationThread(WindowsDgpsGUIFrame* parent, navigation_variables usedVariables)
{}

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

     m_parent->m_navigationThread = NULL;
    //dtor
}

主要内容与问题相同