0
votes

I wrote a class inherited from wxDialog. I'm currently on Ubuntu 12.04, using Code::Blocks with gcc 4.6. My application works fine until I include this class in the project: either the debug and release configuration give the same error. Here's the code:

Header file

#ifndef EBCDIALOG_H
#define EBCDIALOG_H

#include <wx/dialog.h>
#include <wx/sizer.h>
#include <wx/textctrl.h>
#include <wx/button.h>

class EBCDialog : public wxDialog
{
    public:

        EBCDialog(wxWindow* parent, wxWindowID id, const wxString& title );
        ~EBCDialog();

        void OnOK(wxCommandEvent& event);
    private:

       DECLARE_CLASS(EBCDialog)
        DECLARE_EVENT_TABLE()
};

#endif //EBCDIALOG_H

Cpp file:

#include "EBCDialog.h"

EBCDialog::EBCDialog(wxWindow* parent, wxWindowID id, const wxString& title ) : wxDialog(parent, id, title, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
{
    wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL);
    // Create text ctrl with minimal size 100x60
    topSizer->Add(new wxTextCtrl(this, wxID_ANY, wxT("test"), wxDefaultPosition, wxSize(100,60)),
        1,  // make vertically stretchable
        wxEXPAND|   // make horizontally stretchable
        wxALL,      // and make border all around
        10);        // set border width to 10

    wxBoxSizer* buttonSizer = new wxBoxSizer( wxHORIZONTAL );
    buttonSizer->Add(new wxButton(this, wxID_OK, _T("OK")),
        0,  // make horizontally unstretchable
        wxALL,  // make border all around: implicit top alignment
        10);   // set border width to 10

    buttonSizer->Add(new wxButton(this, wxID_CANCEL, _T("Cancel")),
        0,  // make horizontally unstretchable
        wxALL,  // make border all around (implicit top alignment)
        10);   // set border width to 10

    topSizer->Add(buttonSizer,
    0,  // make vertically unstretchable
    wxALIGN_CENTER ); // no border and centre horizontally

    SetSizer( topSizer ); // use the sizer for layout
    topSizer->Fit( this );

    // fit the dialog to the contents
    topSizer->SetSizeHints( this ); // set hints to honor min size
}

EBCDialog::~EBCDialog()
{

}

void EBCDialog::OnOK(wxCommandEvent& event)
{

}

BEGIN_EVENT_TABLE(EBCDialog, wxDialog)
    EVT_BUTTON(wxID_OK, EBCDialog::OnOK)
END_EVENT_TABLE()

Here's Code::Blocks log:

g++ -Wall -I/usr/lib/i386-linux-gnu/wx/include/gtk2-unicode-debug-2.8 -I/usr/include/wx-2.8 -D_FILE_OFFSET_BITS=64 -D_LARGE_FILES -D_WXDEBUG_ -D_WXGTK_ -pthread -O2 -I"/home/angelo/CodeBlocks/Event Bus Configurer/include" -I"/home/angelo/CodeBlocks/Event Bus Configurer/bitmaps" -c "/home/angelo/CodeBlocks/Event Bus Configurer/src/EBCDialog.cpp" -o obj/src/EBCDialog.o g++ -o "bin/Event Bus Configurer" obj/src/EBCApp.o obj/src/EBCDialog.o obj/src/EBCFrame.o obj/src/EBCList.o obj/src/HandlerFile.o -L/usr/lib/i386-linux-gnu -pthread -Wl,-Bsymbolic-functions -Wl,-z,relro -L/usr/lib/i386-linux-gnu -lwx_gtk2ud_richtext-2.8 -lwx_gtk2ud_aui-2.8 -lwx_gtk2ud_xrc-2.8 -lwx_gtk2ud_qa-2.8 -lwx_gtk2ud_html-2.8 -lwx_gtk2ud_adv-2.8 -lwx_gtk2ud_core-2.8 -lwx_baseud_xml-2.8 -lwx_baseud_net-2.8 -lwx_baseud-2.8 -s obj/src/EBCDialog.o:(.rodata._ZTV9EBCDialog[vtable for EBCDialog]+0x8): undefined reference to `EBCDialog::GetClassInfo() const' collect2: ld returned 1 exit status Process terminated with status 1 (0 minutes, 2 seconds) 1 errors, 0 warnings

Any help is appreciated, thank you!

3

3 Answers

1
votes

remove DECLARE_CLASS(EBCDialog) from your code, the base class wxDialog already does that for you.

From the documentation:

wxWidgets uses its own RTTI ("run-time type identification") system which predates the current standard C++ RTTI and so is kept for backwards compatibility reasons but also because it allows some things which the standard RTTI doesn't directly support (such as creating a class from its name).

The standard C++ RTTI can be used in the user code without any problems and in general you shouldn't need to use the functions and the macros in this section unless you are thinking of modifying or adding any wxWidgets classes.

1
votes

I do not think you need DECLARE_CLASS. It is part of a lagacy RTTI system

http://docs.wxwidgets.org/2.8/wx_rttimacros.html

Try removing that line.

1
votes

The rule is simple: if you use DECLARE_CLASS(), you must also use IMPLEMENT_CLASS(). So you need to either remove the former, as advised by the other replies, or add the latter.

And if you don't need to create the objects of this class dynamically (i.e. from name), you don't need it so you should indeed just remove it. However it's really better to understand what you're doing instead of just copying the examples.