How do I instantiate a new class I have written

If you are using wxDev-C++ for your wxWidgets design, please ask your questions here instead of in IDE Related.
Post Reply
archieb
In need of some credit
In need of some credit
Posts: 6
Joined: Sat Jul 21, 2012 4:03 am

How do I instantiate a new class I have written

Post by archieb »

The class is very simple.
A wxWidgets frame program with a few controls that has been written with wxDev-C++, compiles, and runs.
I have the class written in a new file with a header file.
I presume to call it with a wxButton from the main frame.

Thanks,
archieb
jgrzybowski
Earned some good credits
Earned some good credits
Posts: 113
Joined: Sat Sep 24, 2011 9:32 pm
Location: Poland

Re: How do I instantiate a new class I have written

Post by jgrzybowski »

If you want to see files of your class definition inside of Project Inspector widow, you need to do: Project -> Add to Project and select your files cpp and h.

If you want to use object of your class:

Code: Select all

#include YourClass.h
//...
/*
 * YourButtonClick
 */
void YourFrameFrm::YourButtonClick(wxCommandEvent& event)
{
	YourClass YourObject = YourClass(); //construtor
	//...
}
I am not sure if you were looking for these informations above, I hope.

Regards
Jarek
archieb
In need of some credit
In need of some credit
Posts: 6
Joined: Sat Jul 21, 2012 4:03 am

Re: How do I instantiate a new class I have written

Post by archieb »

I am sorry to not have gotten back to this earlier. I could not see the response from the main page of wxDev-C++

The classFile.cpp and its .h file are in the project files listing.
the .h file is included in the program file FrameName.cpp

In FrameName.cpp I put:

Rectangle rectangle1 = Rectangle();

into a button click event callback function.

The compiler complains that it expected ';' before 'rectangle1'

Your comments?

May I also put this constructor into the constructor of the frame for this program as well so that I can reserve the button click callback function for calling methods from the class?

Thanks,
archieb
jgrzybowski
Earned some good credits
Earned some good credits
Posts: 113
Joined: Sat Sep 24, 2011 9:32 pm
Location: Poland

Re: How do I instantiate a new class I have written

Post by jgrzybowski »

Try to move
#include "classFile.h"
form FrameName.cpp to FrameName.h

Regards
Jarek
archieb
In need of some credit
In need of some credit
Posts: 6
Joined: Sat Jul 21, 2012 4:03 am

Re: How do I instantiate a new class I have written

Post by archieb »

I moved class.h to nameFrm.h
The first time it compiled.
so I moved the line
Rectangle rectangle1 = Rectangle();
to the constructor nameFrm::nameFRM()
Then compile failed with the lines
expected ';' before 'rectangle1'
I moved the line back to the button callback and,

The error persists.

The project is now composed of:

nameApp.cpp
nameFrm.wxform
nameFrm.cpp
secondFile.cpp
classFile.cpp
and a header file for each.
nameApp.rc is in the project as well

secondFile.h
classFile.h
are both included in
nameFrm.h

nameApp.h
nameFrm.h
are included in
nameApp.cpp

nameFrm.h
is included in
nameFrm.cpp

#include nameFrm.h
#include <wx/image.h>
#include <wx/listctrl.h>
#include <wx/sizer.h>
#include <wx/log.h>
#include <wx/intl.h>
#include <wx/print.h>
#include <wx/filename.h>
are included in
secondFile.cpp

#include "Project7-4-2Frm.h"
#include <wx/object.h>
are included in
classFile.cpp

The error persists.

The offending line is in a button callback now.
If I comment this one line out, then the program compiles and runs

Any more comments?
Thanks,
archieb
jgrzybowski
Earned some good credits
Earned some good credits
Posts: 113
Joined: Sat Sep 24, 2011 9:32 pm
Location: Poland

Re: How do I instantiate a new class I have written

Post by jgrzybowski »

You can try to use Rebiuld All instead of Compile.

Other options:

Rectagle rectangle1;
rectangle1 = Rectangle();

Rectangle* rectangle1;
rectangle1 = new Rectangle()
//... and when you close the program:
delete rectangle1;

If you still have the error, show us some piece of code with the error.

Regards
Jarek
archieb
In need of some credit
In need of some credit
Posts: 6
Joined: Sat Jul 21, 2012 4:03 am

Re: How do I instantiate a new class I have written

Post by archieb »

I tried each of the several suggestions then zipped up all of the files and included them.
In the zip file there is a file "ScreenShot.pdf" that shows the lines in the constructor for the frame and the error window.
The error reports are the same whether it is in the constructor or in the callback at the bottom of this file.
The class Rectangle is in the file "classFile.cpp"
"secondFile.cpp" contains a couple of functions, one that returns a string and the second that itself writes a string to the forms memo object.
For these I was using BuildAll from the Execute menu.

I am interested that you have not suggested using a script with the word IMPLEMENT in the name.
What does wxDev-C++ use those for?
Where should they be placed among the files?

wxDev-C++ is build 7.4.4.569
Dev-CPP is here: C:\Program Files\Dev-Cpp
wxWidgets is in: C:\Program Files\wxWidgets-2.9.3

Thanks for your help,
archieb
Attachments
wxDev-C++ProjectFiles.zip
Zip of the project directory
(135.08 KiB) Downloaded 176 times
jgrzybowski
Earned some good credits
Earned some good credits
Posts: 113
Joined: Sat Sep 24, 2011 9:32 pm
Location: Poland

Re: How do I instantiate a new class I have written

Post by jgrzybowski »

You had got bad luck with the name "Rectangle". It seems to be a name of function defined in some library for Windows (it could be a file Gdi32.lib). What I have done:
1)I have included your classFile.h and classFile.cpp into my project.
2)In classFile.cpp must be removed lines:
//extern Project7_4_2Frm* frame;
//#include "Project7-4-2Frm.h"
3)In classFile.cpp must be added line:
#include classFile.h
4)I have changed all names "Rectangle" to new unknown for compiler name "mRectangle"
5)I have defined object in my frame constructor:
mRectangle rectangle1 = mRectangle();
just below CreateGUIControls();
6)Compilation took successful;
Regards
Jarek
archieb
In need of some credit
In need of some credit
Posts: 6
Joined: Sat Jul 21, 2012 4:03 am

Re: How do I instantiate a new class I have written

Post by archieb »

Thanks, That works
archieb
Post Reply