朋友班'没有名字的成员......'

时间:2016-11-21 08:13:54

标签: c++ multithreading friend

wxWidgets项目中的主框架在按下按钮时启动一个线程。我试图从wxThread类的文档中使用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;
}

这是主题的标题:

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
}

有谁知道我做错了什么?任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

如错误所示:班级WindowsDgpsGUIFrame拥有会员m_navigationThreadCS。基类wxThread没有。

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

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

由于m_parent的类型为wxFrame*,因此您的错误。

您可以dynamic_castm_parent发送到包含它的派生类,或者只是将该类型更改为WindowsDgpsGUIFrame*

答案 1 :(得分:0)

因此,总结前一个答案的评论,这就是它的工作原理:

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
}

主要内容与问题相同。