Question

I have just started to learn wxWidgets and have zero experience. I was recommended to check this tutorial.
So I created a 'console application' in Code Blocks 13.12 ( Win 7 ) , selected wxWidgets 3.0 , and tried to insert the code from tutorial into my code. The basic code that Code Blocks provided me upon project creation was :

#include <iostream>

using namespace std;

int main()
 {
     // "hello world" with cout
     return 0;
 }

Despite trying to insert the tutorial code in various ways, the program never compiled, I always got many errors. Apart from my lack of skills using wxWidgets, I suspect one of the reasons may be that I have installed wxWidgets 3.0, and tutorial may be using an older version. Can someone please show me how to write this code in a CodeBlocks project to make it build and run successfully ? Thank you.

EDIT :

The code I tried to compile was :

#include <iostream>

using namespace std;

#include <wx/wx.h>


int main()
{

    class OurApp : public wxApp
{
    bool OnInit()
    {
        wxFrame* frame = new wxFrame(NULL, wxID_ANY, "Our First App");
        frame->Show();
        return true;
    }

};

IMPLEMENT_APP(OurApp)

return 0;
}

This particular version returned errors :

N:\projekticpp\wxTestTutorial2\main.cpp|28|error: expected unqualified-id before string constant| N:\projekticpp\wxTestTutorial2\main.cpp|28|error: a function-definition is not allowed here before '{' token| N:\projekticpp\wxTestTutorial2\main.cpp|28|error: a function-definition is not allowed here before '{' token|

But trying other different ways of putting the code together, I got all sorts of different ones.

Was it helpful?

Solution

IMPLEMENT_APP() or, better, wxIMPLEMENT_APP() is a macro which must occur in the global scope, you can't put it inside the main() function.

And, in fact, if you use it, you don't need main() at all as it is generated by this macro expansion.

Finally, while you didn't hit this problem yet, once the code compiles it probably won't link because you chose the wrong application type: it should be a "GUI" (or maybe "Windows") and not a "console" application.

OTHER TIPS

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top