Class A declares Class B as a friend, but Class B has Class A as a member

59 Views Asked by At

So I've got a bit of a chicken-and-egg situation where:

class MyWindow;

class MyPanel : public wxPanel
{
public:
    MyPanel(wxWindow* parent, wxWindowID id = wxID_ANY, const wxSize& size = wxDefaultSize, long style = wxTAB_TRAVERSAL);
    void OnMouseLeftDown(wxMouseEvent& evt);
    void OnMouseLeftUp(wxMouseEvent&);
    void OnMotion(wxMouseEvent& evt);
    void OnPaint(wxPaintEvent&);
    void AddPoint(const wxPoint& point);
    void ClearDrawing();
    friend void MyWindow::Submit(wxCommandEvent& event);
private:
    typedef std::vector<wxPoint> Line;
    typedef std::vector<Line> Lines;
    Lines lines;
    wxBitmap bm;

    wxDECLARE_EVENT_TABLE();
};

class MyWindow : public wxWindow
{
public:
    MyWindow(wxWindow* parent, wxWindowID id = wxID_ANY, const wxSize& size = wxDefaultSize, const wxPoint& pos = wxDefaultPosition, long style = wxTAB_TRAVERSAL);
    void Clear(wxCommandEvent& event);
    void Submit(wxCommandEvent& event);
private:
    MyPanel canvas;
    wxDECLARE_EVENT_TABLE();
};

Where I would like MyPanel to have MyWindow::Submit() be a friend, but also, MyWindow has MyPanel as a class member, so no matter which I put before the other, or which I forward-declare, I get some error. The code in its current state above leads to the following error:

C2027   use of undefined type 'MyWindow'

The window is a parent window to the panel, but the panel maintains its own bitmap, and the window needs access to this bitmap for event-handling purposes.

This feels like a pretty hairy problem, the solution to which can probably be only found in the Standard. So that I don't have to spend 300 dollars on the standard, or perhaps several hours leafing through to find exactly this issue, could I get a consultation on how to most elegantly handle this? My language standard is C++14.

0

There are 0 best solutions below