Can't compile this example.

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
wx_n00b
In need of some credit
In need of some credit
Posts: 9
Joined: Thu Sep 05, 2013 12:07 am

Can't compile this example.

Post by wx_n00b » Sun Sep 15, 2013 1:10 pm

Hello, this is my dilemma. When I try to compile the below code, this is the error that I get:

Code: Select all

$ g++ main_custom.cpp main_custom.h widget.cpp widget.h burning.cpp burning.h `wx-config --libs` `wx-config --cxxflags` -o main_custom
widget.cpp: In constructor ‘Widget::Widget(wxPanel*, int)’:
widget.cpp:6:5: error: type ‘wxFrame’ is not a direct base of ‘Widget’
widget.cpp:8:55: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
widget.cpp:8:55: error: cannot convert ‘<brace-enclosed initializer list>’ to ‘int’ in assignment
burning.cpp: In constructor ‘Burning::Burning(const wxString&)’:
burning.cpp:35:22: error: ‘wxEVT_COMMAND_SLIDER_UPDATE’ was not declared in this scope
burning.cpp: At global scope:
burning.cpp:47:6: error: prototype for ‘void Burning::GetCurWidth()’ does not match any in class ‘Burning’
In file included from burning.cpp:3:0:
burning.h:14:7: error: candidate is: int Burning::GetCurWidth()
What am I screwing up?

widget.h:

Code: Select all

#ifndef __WIDGET_H__
#define __WIDGET_H__

#include <wx/wx.h>

class Widget : public wxPanel
{
public:
  Widget(wxPanel * parent, int id);

  void OnSize(wxSizeEvent & event);
  void OnPaint(wxPaintEvent & event);

private:
  int asize;

  static int num[9];

  wxPanel * m_parent;
};

#endif
widget.cpp:

Code: Select all

#include "burning.h"

Widget::Widget(wxPanel * parent, int id)
  : wxFrame(parent, id, wxDefaultPosition, wxSize(-1, 30), wxSUNKEN_BORDER)
{
  num[9] = {75, 150, 225, 300, 375, 450, 525, 600, 675};
  asize = sizeof(num) / sizeof(num[1]);

  m_parent = parent;

  Connect(wxEVT_PAINT, wxPaintEventHandler(Widget::OnPaint));
  Connect(wxEVT_SIZE, wxSizeEventHandler(Widget::OnSize));
}

void Widget::OnPaint(wxPaintEvent & event)
{
  wxFont font(9, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
    wxFONTWEIGHT_NORMAL, false, wxT("Courier 10 Pitch"));
  wxPaintDC dc(this);
  dc.SetFont(font);

  wxSize size = GetSize();
  int width = size.GetWidth();

  Burning * burn = (Burning * )m_parent -> GetParent();

  int cur_width = burn -> GetCurWidth();

  int step = (int)round(width / 10.0);

  int till = (int)((width / 750.0) * cur_width);
  int full = (int)((width / 750.0) * 700);

  if (cur_width >= 700)
  {
    dc.SetPen(wxPen(wxColour(255, 255, 184)));
    dc.SetBrush(wxBrush(wxColour(255, 255, 184)));
    dc.DrawRectangle(0, 0, full, 30);

    dc.SetPen(wxPen(wxColour(255, 175, 175)));
    dc.SetBrush(wxBrush(wxColour(255, 175, 175)));
    dc.DrawRectangle(full, 0, till - full, 30);
  }
  else
  {
    dc.SetPen(wxPen(wxColour(255, 255, 184)));
    dc.SetBrush(wxBrush(wxColour(255, 255, 184)));
    dc.DrawRectangle(0, 0, full, 30);
  }

  dc.SetPen(wxPen(wxColour(90, 80, 60)));

  for (int i = 1; i < asize; i++)
  {
    dc.DrawLine(i * step, 0, i * step, 6);
    wxSize size = dc.GetTextExtent(wxString::Format(wxT("%d"), num[i - 1]));
    dc.DrawText(wxString::Format(wxT("%d"), num[i - 1]),
      i * step - size.GetWidth() / 2, 8);
  }
}

void Widget::OnSize(wxSizeEvent & event)
{
  Refresh();
}
burning.h:

Code: Select all

#ifndef __BURNING_H__
#define __BURNING_H__

#include "widget.h"

class Burning : public wxFrame
{
public:
  Burning(const wxString & title);

  void OnScroll(wxScrollEvent & event);
  int GetCurWidth();

private:
  int cur_width;

  static const int ID_SLIDER = 1;

  wxSlider * m_slider;

  Widget * m_wid;
};

#endif
burning.cpp:

Code: Select all

#include "burning.h"

Burning::Burning(const wxString & title)
  : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(350, 200))
{
  cur_width = 75;

  wxPanel * panel = new wxPanel(this, wxID_ANY);
  wxPanel * centerPanel = new wxPanel(panel, wxID_ANY);

  m_slider = new wxSlider(centerPanel, ID_SLIDER, 75, 0, 750, wxPoint(-1, -1),
    wxSize(150, -1), wxSL_LABELS);

  wxBoxSizer * vbox = new wxBoxSizer(wxVERTICAL);
  wxBoxSizer * hbox = new wxBoxSizer(wxHORIZONTAL);
  wxBoxSizer * hbox2 = new wxBoxSizer(wxHORIZONTAL);
  wxBoxSizer * hbox3 = new wxBoxSizer(wxHORIZONTAL);

  m_wid = new Widget(panel, wxID_ANY);
  hbox -> Add(m_wid, 1, wxEXPAND);

  hbox2 -> Add(centerPanel, 1, wxEXPAND);
  hbox3 -> Add(m_slider, 0, wxTOP | wxLEFT, 35);

  centerPanel -> SetSizer(hbox3);

  vbox -> Add(hbox2, 1, wxEXPAND);
  vbox -> Add(hbox, 0, wxEXPAND);

  panel -> SetSizer(vbox);
  m_slider -> SetFocus();

  Connect(ID_SLIDER, wxEVT_COMMAND_SLIDER_UPDATE,
    wxScrollEventHandler(Burning::OnScroll));

  Centre();
}

void Burning::OnScroll(wxScrollEvent & WXUNUSED(event))
{
  cur_width = m_slider -> GetValue();
  m_wid -> Refresh();
}

void Burning::GetCurWidth()
{
  return cur_width;
}
main_custom.h:

Code: Select all

#ifndef __MAIN_CUSTOM_H__
#define __MAIN_CUSTOM_H__

#include <wx/wx.h>

class MyApp : public wxApp
{
public:
  virtual bool OnInit();
};

#endif
main_custom.cpp:

Code: Select all

#include "main_custom.h"
#include "burning.h"

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{
  Burning * burning = new Burning(wxT("The Burning Widget"));
  burning -> Show(true);

  return true;
}

User avatar
doublemax
Moderator
Moderator
Posts: 14079
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Can't compile this example.

Post by doublemax » Sun Sep 15, 2013 1:21 pm

Code: Select all

class Widget : public wxPanel

Code: Select all

Widget::Widget(wxPanel * parent, int id)
  : wxFrame(parent, id, wxDefaultPosition, wxSize(-1, 30), wxSUNKEN_BORDER)
I think the error message is pretty clear. You use wxPanel as base class first and then call the wxFrame base ctor.
Use the source, Luke!

Post Reply