Function of class doesn't work

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
Post Reply
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 675
Joined: Tue Jul 26, 2016 2:00 pm

Function of class doesn't work

Post by Wanderer82 »

Sorry for the weird subject title.

I'm completely new to classes. My question: I created a class "Produkt.h"

Code: Select all

#ifndef PRODUKT_H
#define PRODUKT_H

#include <string>
#include <wx/msgdlg.h>

using namespace std;

class Produkt
{
    public:

        Produkt();
        virtual ~Produkt();
        string Name;

        void setName(string v)
        {
            Name = v;
        }

        void getName()
        {
            wxMessageBox(Name);
        }
    protected:

    private:
};

#endif // PRODUKT_H
Now I try to create an object in my main program. As long as I put the command in an OnClickButton for example, everything works fine.

Code: Select all

void Klassen_TestFrame::OnButton1Click(wxCommandEvent& event)
{
  Produkt Produkt1;
  Produkt1.setName("Hallo");
  Produkt1.getName();
}
But what I want to do is, create the object not only inside this "OnButton1Click" but so that I can use it everywhere in my program. Concretely: I want to create the object and set its name. But then I want to get the name anywhere in my program. But when I create the object outside "OnButton1Click" I get an error: 'Produkt1' does not name a type".

Thanks for any help.

Thomas

PS: I tried this in a console application inside the "int main" function. No problem there, so it has to do something with the wxWidgets project and where I place the code.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Function of class doesn't work

Post by doublemax »

You've probably heard already that using global variables is bad, but let's go with it ;)

First you need to define the variable, this can happen in any .cpp file, but it must be outside any function/method.

Code: Select all

#include "product.h"
Produkt g_Produkt1;         // g_ prefix is often used to indicate that it's a global variable.
With this you will be able to access that variable from anywhere in the same source file.

But if you want to access it from any other file, you must tell the compiler that this variable exists.
Add the following line near the end of "product.h":

Code: Select all

extern Produkt g_Produkt1;
Now if you include "product.h" anywhere, you can also access the global variable.
Use the source, Luke!
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 675
Joined: Tue Jul 26, 2016 2:00 pm

Re: Function of class doesn't work

Post by Wanderer82 »

That's what I did actually. I have a header file "Produkt.h":

Code: Select all

#include <string>
using namespace std;


class Produkt
{

public:

string Produktbild;
string Kategorie;
int Anzahl_Lager;
int Anzahl_disponiert;

void setKategorie( string v )
{
    Kategorie = v;
}

void readKategorie()
{
    wxMessageBox(Kategorie);
}

};
Then I have my xymain.cpp file. Even if I put the code directly after the #include command I get the same error.

Code: Select all

#include "Wall_MatMain.h"
#include <wx/msgdlg.h>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include "Produkt.h"
#include "wx/textdlg.h"

using namespace std;

Produkt Produkt1;
Produkt1.setKategorie("Hallo");
As to your remark about global variables being bad... My program is kind of a data base. I want to write a program for a school to make orders for material you use during the school year like ring binders, sheets, pencils etc. Until now we have a list that every teacher has to fill out. It would be easier to do this in a program like a shop. You can choose your material and put it into a shopping cart. So I need to declare a lot of different material (products). So that's what I wanted to do using a class for the products. The program should - during startup - read all the materials from .txt-files to make objects. The administrator can add products this way.
Last edited by Wanderer82 on Sun Sep 03, 2017 2:14 pm, edited 2 times in total.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Function of class doesn't work

Post by doublemax »

Code: Select all

Produkt1.setKategorie("Hallo");
You can't have code "in the wild", outside any function.

If you want to initialize the variable, pass a parameter to the constructor or do it early in your program, e.g. in main() or wxApp::OnInit()
Use the source, Luke!
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 675
Joined: Tue Jul 26, 2016 2:00 pm

Re: Function of class doesn't work

Post by Wanderer82 »

Oh, I think that's a problem I've already asked about. I need some kind of "starter function" that runs when I start my program.

I managed to do it with your previous help using the MyWorkerFunction.

Now I wonder what would be a better solution to my intention than using global objects.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Function of class doesn't work

Post by doublemax »

Now I wonder what would be a better solution to my intention than using global objects.
It sounds like a smaller project. I'd say usually you would make the "database" object a member of the main frame and you would initialize it in the constructor of the frame.
Use the source, Luke!
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 675
Joined: Tue Jul 26, 2016 2:00 pm

Re: Function of class doesn't work

Post by Wanderer82 »

I want to use a wxdialog for each product category which should have access to the information out of the product objects that have been read from the text files in the main wxframe (when starting the program). Is there a way to make objects available / readable in other wxdialogs than the main wxframe?
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Function of class doesn't work

Post by doublemax »

That's a common problem and typical solutions are:

1) add a getter method to MyFrame (or whatever the class is called in your case) to access the "database" object. As you usually pass the frame pointer to the dialog constructor anyway (as parent), this will give you access to the frame (and therefore its methods) everywhere in the dialog

2) pass a pointer/reference to the "database" object to the dialog constructor

3) a singleton. Actually still a global variable but with some sugar-coating to make it less "dangerous" and keep teachers happy ;) (Google "singleton pattern" for further explanation). It sounds more complicated as it is and usually requires only a few lines of code to implement.

4) access though an already existing global variable:
In wxWidgets there is already a global variable, the current wxApp instance. So you could make your "database" object a member of "MyApp" (or whatever it's called in your case) and add a getter method to get a pointer/reference to the instance

5) a simple global variable as you have now


The passing around of pointer (#1 and #2) is the most common solution, but it can get a bit annoying when you have to pass it over several hierarchy levels from object to object.
Use the source, Luke!
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 675
Joined: Tue Jul 26, 2016 2:00 pm

Re: Function of class doesn't work

Post by Wanderer82 »

Well as I'm a beginner (especially with pointers and classes) the options 1-4 (more or less) are no option at this point. Maybe no 4. I have to look into this first.

But for no 5... if I already have the global variable I still need to know how to access it in another dialog. That's what I actually wanted to ask ;-)
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Function of class doesn't work

Post by doublemax »

But for no 5... if I already have the global variable I still need to know how to access it in another dialog. That's what I actually wanted to ask
If you do it like i wrote in #p179293, you should be able to access the global variable from anywhere, you just have to include "Produkt.h" in the files where you want to use it.
Use the source, Luke!
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 675
Joined: Tue Jul 26, 2016 2:00 pm

Re: Function of class doesn't work

Post by Wanderer82 »

Okay, tried that but still get "undefined reference to 'Produkt1'.


Main.h:

Code: Select all

#include "Wall_MatMain.h"
#include <wx/msgdlg.h>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include "Produkt.h"
#include "Papier.h"
#include "wx/textdlg.h"


using namespace std;

Produkt Produkt[50];


//(*InternalHeaders(Wall_MatFrame)
#include <wx/bitmap.h>
#include <wx/font.h>
#include <wx/intl.h>
#include <wx/image.h>
#include <wx/string.h>
//*)

void Produkte_einlesen()
{
        string Info[100];
        int i = 1;

        int o = 0;
        do
        {
            ifstream Produkte;
            ostringstream Str;
            Str << i;
            string s(Str.str());
            Produkte.open( "C:\\programmieren\\produkt" + s + ".txt" );
            Produkte >> Info[o];
            Produkt[i].setName (Info[o]);
            o = o + 1;
            Produkte >> Info[o];
            Produkt[i].setProduktbild (Info[o]);
            o = o + 1;
            Produkte >> Info[o];
            Produkt[i].setKategorie (Info[o]);
            o = o + 1;
            Produkte >> Info[o];
            string AlsString(Info[o]);
            stringstream Str2;
            Str2 << AlsString;
            int d;
            Str2 >> d;
            Produkt[i].setAnzahl_Lager (d);
            o = o + 1;
            Produkte >> Info[o];
            string AlsString2(Info[o]);
            stringstream Str3;
            Str3 << AlsString2;
            int d2;
            Str3 >> d2;
            Produkt[i].setAnzahl_disponiert(d2);
            i = i + 1;
        }
        while (i < 13);




}


//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(Wall_MatFrame)
const long Wall_MatFrame::ID_STATICBITMAP1 = wxNewId();
const long Wall_MatFrame::ID_BUTTON1 = wxNewId();
const long Wall_MatFrame::ID_STATICTEXT1 = wxNewId();
const long Wall_MatFrame::ID_BITMAPBUTTON1 = wxNewId();
const long Wall_MatFrame::ID_BITMAPBUTTON8 = wxNewId();
const long Wall_MatFrame::ID_BITMAPBUTTON9 = wxNewId();
const long Wall_MatFrame::ID_BITMAPBUTTON2 = wxNewId();
const long Wall_MatFrame::ID_BITMAPBUTTON3 = wxNewId();
const long Wall_MatFrame::ID_BITMAPBUTTON4 = wxNewId();
const long Wall_MatFrame::ID_BITMAPBUTTON5 = wxNewId();
const long Wall_MatFrame::ID_BITMAPBUTTON6 = wxNewId();
const long Wall_MatFrame::ID_BITMAPBUTTON7 = wxNewId();
const long Wall_MatFrame::ID_PANEL1 = wxNewId();
const long Wall_MatFrame::idMenuQuit = wxNewId();
const long Wall_MatFrame::idMenuAbout = wxNewId();
const long Wall_MatFrame::ID_STATUSBAR1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(Wall_MatFrame,wxFrame)
    //(*EventTable(Wall_MatFrame)
    //*)
END_EVENT_TABLE()

Wall_MatFrame::Wall_MatFrame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(Wall_MatFrame)
    wxMenuItem* MenuItem2;
    wxMenuItem* MenuItem1;
    wxFlexGridSizer* FlexGridSizer2;
    wxBoxSizer* BoxSizer2;
    wxMenu* Menu1;
    wxBoxSizer* BoxSizer1;
    wxMenuBar* MenuBar1;
    wxFlexGridSizer* FlexGridSizer1;
    wxBoxSizer* BoxSizer3;
    wxMenu* Menu2;

    Create(parent, wxID_ANY, _("Wall-Mat"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("wxID_ANY"));
    BoxSizer1 = new wxBoxSizer(wxVERTICAL);
    Panel1 = new wxPanel(this, ID_PANEL1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
    Panel1->SetBackgroundColour(wxColour(217,217,255));
    FlexGridSizer1 = new wxFlexGridSizer(0, 3, 0, 0);
    BoxSizer2 = new wxBoxSizer(wxVERTICAL);
    BoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
    StaticBitmap1 = new wxStaticBitmap(Panel1, ID_STATICBITMAP1, wxBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\wallmat.png"))), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICBITMAP1"));
    BoxSizer3->Add(StaticBitmap1, 0, wxALL|wxALIGN_CENTER_VERTICAL, wxDLG_UNIT(Panel1,wxSize(5,0)).GetWidth());
    BoxSizer3->Add(121,49,0, wxALL|wxEXPAND, wxDLG_UNIT(Panel1,wxSize(5,0)).GetWidth());
    Button1 = new wxButton(Panel1, ID_BUTTON1, _("Warenkorb"), wxDefaultPosition, wxSize(155,40), 0, wxDefaultValidator, _T("ID_BUTTON1"));
    wxFont Button1Font(14,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Arial"),wxFONTENCODING_DEFAULT);
    Button1->SetFont(Button1Font);
    BoxSizer3->Add(Button1, 0, wxALL|wxALIGN_CENTER_VERTICAL, wxDLG_UNIT(Panel1,wxSize(0,0)).GetWidth());
    StaticText1 = new wxStaticText(Panel1, ID_STATICTEXT1, _("0"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
    wxFont StaticText1Font(12,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Calibri"),wxFONTENCODING_DEFAULT);
    StaticText1->SetFont(StaticText1Font);
    BoxSizer3->Add(StaticText1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer2->Add(BoxSizer3, 0, wxALL|wxEXPAND, 5);
    FlexGridSizer2 = new wxFlexGridSizer(0, 3, 0, 0);
    FlexGridSizer2->Add(-1,-1,1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    FlexGridSizer2->Add(-1,-1,1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    FlexGridSizer2->Add(-1,-1,1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BitmapButton1 = new wxBitmapButton(Panel1, ID_BITMAPBUTTON1, wxBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\papier2.jpg"))), wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW, wxDefaultValidator, _T("ID_BITMAPBUTTON1"));
    FlexGridSizer2->Add(BitmapButton1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, wxDLG_UNIT(Panel1,wxSize(5,0)).GetWidth());
    BitmapButton8 = new wxBitmapButton(Panel1, ID_BITMAPBUTTON8, wxBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\geometrie.jpg"))), wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW, wxDefaultValidator, _T("ID_BITMAPBUTTON8"));
    FlexGridSizer2->Add(BitmapButton8, 1, wxALL, wxDLG_UNIT(Panel1,wxSize(5,0)).GetWidth());
    BitmapButton9 = new wxBitmapButton(Panel1, ID_BITMAPBUTTON9, wxBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\kleben.jpg"))), wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW, wxDefaultValidator, _T("ID_BITMAPBUTTON9"));
    FlexGridSizer2->Add(BitmapButton9, 1, wxALL, wxDLG_UNIT(Panel1,wxSize(5,0)).GetWidth());
    BitmapButton2 = new wxBitmapButton(Panel1, ID_BITMAPBUTTON2, wxBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\papier2.jpg"))), wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW, wxDefaultValidator, _T("ID_BITMAPBUTTON2"));
    FlexGridSizer2->Add(BitmapButton2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BitmapButton3 = new wxBitmapButton(Panel1, ID_BITMAPBUTTON3, wxBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\papier2.jpg"))), wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW, wxDefaultValidator, _T("ID_BITMAPBUTTON3"));
    FlexGridSizer2->Add(BitmapButton3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BitmapButton4 = new wxBitmapButton(Panel1, ID_BITMAPBUTTON4, wxBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\papier2.jpg"))), wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW, wxDefaultValidator, _T("ID_BITMAPBUTTON4"));
    FlexGridSizer2->Add(BitmapButton4, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BitmapButton5 = new wxBitmapButton(Panel1, ID_BITMAPBUTTON5, wxBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\papier2.jpg"))), wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW, wxDefaultValidator, _T("ID_BITMAPBUTTON5"));
    FlexGridSizer2->Add(BitmapButton5, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BitmapButton6 = new wxBitmapButton(Panel1, ID_BITMAPBUTTON6, wxBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\papier2.jpg"))), wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW, wxDefaultValidator, _T("ID_BITMAPBUTTON6"));
    FlexGridSizer2->Add(BitmapButton6, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BitmapButton7 = new wxBitmapButton(Panel1, ID_BITMAPBUTTON7, wxBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\papier2.jpg"))), wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW, wxDefaultValidator, _T("ID_BITMAPBUTTON7"));
    FlexGridSizer2->Add(BitmapButton7, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    BoxSizer2->Add(FlexGridSizer2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    FlexGridSizer1->Add(BoxSizer2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
    Panel1->SetSizer(FlexGridSizer1);
    FlexGridSizer1->Fit(Panel1);
    FlexGridSizer1->SetSizeHints(Panel1);
    BoxSizer1->Add(Panel1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
    SetSizer(BoxSizer1);
    MenuBar1 = new wxMenuBar();
    Menu1 = new wxMenu();
    MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
    Menu1->Append(MenuItem1);
    MenuBar1->Append(Menu1, _("&File"));
    Menu2 = new wxMenu();
    MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
    Menu2->Append(MenuItem2);
    MenuBar1->Append(Menu2, _("Help"));
    SetMenuBar(MenuBar1);
    StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
    int __wxStatusBarWidths_1[1] = { -1 };
    int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
    StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
    StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
    SetStatusBar(StatusBar1);
    BoxSizer1->Fit(this);
    BoxSizer1->SetSizeHints(this);

    Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Wall_MatFrame::OnButton1Click);
    Connect(ID_BITMAPBUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Wall_MatFrame::OnBitmapButton1Click);
    Connect(ID_BITMAPBUTTON8,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Wall_MatFrame::OnBitmapButton8Click);
    Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&Wall_MatFrame::OnQuit);
    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&Wall_MatFrame::OnAbout);
    Connect(wxID_ANY,wxEVT_CLOSE_WINDOW,(wxObjectEventFunction)&Wall_MatFrame::OnClose);
    //*)

    CallAfter( MyWorkerFunction );
}

void Wall_MatFrame::MyWorkerFunction()
{

    Produkte_einlesen();

}


Wall_MatFrame::~Wall_MatFrame()
{


    //(*Destroy(Wall_MatFrame)
    //*)
}

void Wall_MatFrame::OnQuit(wxCommandEvent& event)
{
    Close();
}

void Wall_MatFrame::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}

void Wall_MatFrame::OnClose(wxCloseEvent& event)
{
     Destroy();
}

void Wall_MatFrame::OnBitmapButton1Click(wxCommandEvent& event)
{
}

void Wall_MatFrame::OnBitmapButton8Click(wxCommandEvent& event)
{

    Papier dialog(this);

    dialog.ShowModal();

}

void Wall_MatFrame::OnButton1Click(wxCommandEvent& event)
{
        Produkt[2].readName();
        Produkt[2].readProduktbild();
        Produkt[2].readAnzahl_Lager();
        Produkt[10].readAnzahl_disponiert();
}
Produkt.h:

Code: Select all

#include <string>

using namespace std;


class Produkt
{

public:

string Name;
string Produktbild;
string Kategorie;
int Anzahl_Lager;
int Anzahl_disponiert;

void setKategorie( string v )
{
    Kategorie = v;
}

void setProduktbild( string w )
{
    Produktbild = w;
}

void setAnzahl_Lager( int x )
{
    Anzahl_Lager = x;
}

void setAnzahl_disponiert( int y)
{
    Anzahl_disponiert = y;
}

void setName( string z )
{
    Name = z;
}

void readKategorie()
{
    wxMessageBox(Kategorie);
}

void readName()
{
    wxMessageBox(Name);
}

void readProduktbild()
{
    wxMessageBox(Produktbild);
}

void readAnzahl_Lager()
{
    wxString theStr;
    theStr << Anzahl_Lager;
    wxMessageBox(theStr);
}

void readAnzahl_disponiert()
{
    wxString theStr;
    theStr << Anzahl_disponiert;
    wxMessageBox(theStr);
}

};

extern Produkt Produkt1;


Papier.cpp (the dialog):

Code: Select all

#include "Papier.h"
#include <wx/msgdlg.h>
#include "Produkt.h"

//(*InternalHeaders(Papier)
#include <wx/bitmap.h>
#include <wx/font.h>
#include <wx/intl.h>
#include <wx/image.h>
#include <wx/string.h>
//*)

//(*IdInit(Papier)
const long Papier::ID_STATICTEXT1 = wxNewId();
const long Papier::ID_STATICBITMAP1 = wxNewId();
const long Papier::ID_CHOICE1 = wxNewId();
const long Papier::ID_SPINCTRL1 = wxNewId();
const long Papier::ID_BITMAPBUTTON1 = wxNewId();
const long Papier::ID_STATICTEXT2 = wxNewId();
const long Papier::ID_STATICBITMAP2 = wxNewId();
const long Papier::ID_CHOICE2 = wxNewId();
const long Papier::ID_SPINCTRL2 = wxNewId();
const long Papier::ID_BITMAPBUTTON2 = wxNewId();
const long Papier::ID_STATICTEXT3 = wxNewId();
const long Papier::ID_STATICBITMAP3 = wxNewId();
const long Papier::ID_CHOICE4 = wxNewId();
const long Papier::ID_CHOICE3 = wxNewId();
const long Papier::ID_SPINCTRL3 = wxNewId();
const long Papier::ID_BITMAPBUTTON4 = wxNewId();
const long Papier::ID_BITMAPBUTTON3 = wxNewId();
const long Papier::ID_PANEL1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(Papier,wxDialog)
	//(*EventTable(Papier)
	//*)
END_EVENT_TABLE()

Papier::Papier(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size)
{
	//(*Initialize(Papier)
	wxFlexGridSizer* FlexGridSizer2;
	wxBoxSizer* BoxSizer2;
	wxBoxSizer* BoxSizer1;
	wxFlexGridSizer* FlexGridSizer1;

	Create(parent, wxID_ANY, _("Papier / Einzelblätter"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("wxID_ANY"));
	BoxSizer1 = new wxBoxSizer(wxVERTICAL);
	Panel1 = new wxPanel(this, ID_PANEL1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
	FlexGridSizer1 = new wxFlexGridSizer(0, 3, 0, 0);
	BoxSizer2 = new wxBoxSizer(wxVERTICAL);
	FlexGridSizer2 = new wxFlexGridSizer(0, 6, 0, 0);
	StaticText1 = new wxStaticText(Panel1, ID_STATICTEXT1, _("Liniert"), wxDefaultPosition, wxSize(56,18), 0, _T("ID_STATICTEXT1"));
	wxFont StaticText1Font(11,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Calibri"),wxFONTENCODING_DEFAULT);
	StaticText1->SetFont(StaticText1Font);
	FlexGridSizer2->Add(StaticText1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticBitmap1 = new wxStaticBitmap(Panel1, ID_STATICBITMAP1, wxBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\Wall-Mat\\index.jpg"))), wxDefaultPosition, wxDefaultSize, wxNO_BORDER, _T("ID_STATICBITMAP1"));
	FlexGridSizer2->Add(StaticBitmap1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	Choice1 = new wxChoice(Panel1, ID_CHOICE1, wxDefaultPosition, wxSize(72,21), 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE1"));
	Choice1->Append(_("A5"));
	Choice1->SetSelection( Choice1->Append(_("A4")) );
	Choice1->Append(_("A3"));
	Choice1->Append(_("B5"));
	FlexGridSizer2->Add(Choice1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	FlexGridSizer2->Add(-1,-1,1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	SpinCtrl1 = new wxSpinCtrl(Panel1, ID_SPINCTRL1, _T("0"), wxDefaultPosition, wxSize(66,21), 0, 0, 100, 0, _T("ID_SPINCTRL1"));
	SpinCtrl1->SetValue(_T("0"));
	FlexGridSizer2->Add(SpinCtrl1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	BitmapButton1 = new wxBitmapButton(Panel1, ID_BITMAPBUTTON1, wxBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\Wall-Mat\\warenkorb.png"))), wxDefaultPosition, wxDefaultSize, wxNO_BORDER, wxDefaultValidator, _T("ID_BITMAPBUTTON1"));
	FlexGridSizer2->Add(BitmapButton1, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText2 = new wxStaticText(Panel1, ID_STATICTEXT2, _("Kariert"), wxDefaultPosition, wxSize(56,18), 0, _T("ID_STATICTEXT2"));
	wxFont StaticText2Font(11,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Calibri"),wxFONTENCODING_DEFAULT);
	StaticText2->SetFont(StaticText2Font);
	FlexGridSizer2->Add(StaticText2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticBitmap2 = new wxStaticBitmap(Panel1, ID_STATICBITMAP2, wxBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\Wall-Mat\\kariert.jpg"))), wxDefaultPosition, wxDefaultSize, wxSIMPLE_BORDER, _T("ID_STATICBITMAP2"));
	FlexGridSizer2->Add(StaticBitmap2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	Choice2 = new wxChoice(Panel1, ID_CHOICE2, wxDefaultPosition, wxSize(72,21), 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE2"));
	Choice2->Append(_("A5"));
	Choice2->SetSelection( Choice2->Append(_("A4")) );
	Choice2->Append(_("A3"));
	Choice2->Append(_("B5"));
	FlexGridSizer2->Add(Choice2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	FlexGridSizer2->Add(-1,-1,1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	SpinCtrl2 = new wxSpinCtrl(Panel1, ID_SPINCTRL2, _T("0"), wxDefaultPosition, wxSize(66,21), 0, 0, 100, 0, _T("ID_SPINCTRL2"));
	SpinCtrl2->SetValue(_T("0"));
	FlexGridSizer2->Add(SpinCtrl2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	BitmapButton2 = new wxBitmapButton(Panel1, ID_BITMAPBUTTON2, wxBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\Wall-Mat\\warenkorb.png"))), wxDefaultPosition, wxDefaultSize, wxNO_BORDER, wxDefaultValidator, _T("ID_BITMAPBUTTON2"));
	FlexGridSizer2->Add(BitmapButton2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticText3 = new wxStaticText(Panel1, ID_STATICTEXT3, _("Blanko"), wxDefaultPosition, wxSize(56,18), 0, _T("ID_STATICTEXT3"));
	wxFont StaticText3Font(11,wxFONTFAMILY_SWISS,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_BOLD,false,_T("Calibri"),wxFONTENCODING_DEFAULT);
	StaticText3->SetFont(StaticText3Font);
	FlexGridSizer2->Add(StaticText3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	StaticBitmap3 = new wxStaticBitmap(Panel1, ID_STATICBITMAP3, wxBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\Wall-Mat\\farbiga4.png")).Rescale(wxSize(96,109).GetWidth(),wxSize(96,109).GetHeight())), wxDefaultPosition, wxSize(96,109), wxSIMPLE_BORDER, _T("ID_STATICBITMAP3"));
	FlexGridSizer2->Add(StaticBitmap3, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	Choice4 = new wxChoice(Panel1, ID_CHOICE4, wxDefaultPosition, wxSize(72,21), 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE4"));
	Choice4->Append(_("A5"));
	Choice4->SetSelection( Choice4->Append(_("A4")) );
	Choice4->Append(_("A3"));
	Choice4->Append(_("B5"));
	FlexGridSizer2->Add(Choice4, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	Choice3 = new wxChoice(Panel1, ID_CHOICE3, wxDefaultPosition, wxDefaultSize, 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE3"));
	Choice3->SetSelection( Choice3->Append(_("weiss")) );
	Choice3->Append(_("gelb"));
	Choice3->Append(_("blau"));
	Choice3->Append(_("rot"));
	Choice3->Append(_("grün"));
	Choice3->Append(_("schwarz"));
	FlexGridSizer2->Add(Choice3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	SpinCtrl3 = new wxSpinCtrl(Panel1, ID_SPINCTRL3, _T("0"), wxDefaultPosition, wxSize(66,21), 0, 0, 100, 0, _T("ID_SPINCTRL3"));
	SpinCtrl3->SetValue(_T("0"));
	FlexGridSizer2->Add(SpinCtrl3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	BitmapButton4 = new wxBitmapButton(Panel1, ID_BITMAPBUTTON4, wxBitmap(wxImage(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\Wall-Mat\\warenkorb.png"))), wxDefaultPosition, wxDefaultSize, wxNO_BORDER, wxDefaultValidator, _T("ID_BITMAPBUTTON4"));
	FlexGridSizer2->Add(BitmapButton4, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	BitmapButton3 = new wxBitmapButton(Panel1, ID_BITMAPBUTTON3, wxNullBitmap, wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW, wxDefaultValidator, _T("ID_BITMAPBUTTON3"));
	FlexGridSizer2->Add(BitmapButton3, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	BoxSizer2->Add(FlexGridSizer2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	FlexGridSizer1->Add(BoxSizer2, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
	Panel1->SetSizer(FlexGridSizer1);
	FlexGridSizer1->Fit(Panel1);
	FlexGridSizer1->SetSizeHints(Panel1);
	BoxSizer1->Add(Panel1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
	SetSizer(BoxSizer1);
	BoxSizer1->Fit(this);
	BoxSizer1->SetSizeHints(this);

	Connect(ID_BITMAPBUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Papier::OnBitmapButton1Click);
	Connect(wxID_ANY,wxEVT_INIT_DIALOG,(wxObjectEventFunction)&Papier::OnInit);
	//*)
}

Papier::~Papier()
{
	//(*Destroy(Papier)
	//*)
}


void Papier::OnInit(wxInitDialogEvent& event)
{

}

void Papier::OnSpinCtrl1Change(wxSpinEvent& event)
{
}

void Papier::OnBitmapButton1Click(wxCommandEvent& event)
{
    Produkt1.readName();
}
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Function of class doesn't work

Post by doublemax »

Okay, tried that but still get "undefined reference to 'Produkt1'.
That's because you haven't defined the variable "Produkt1" anywhere.

All you have is:

Code: Select all

Produkt Produkt[50];
... which is also bad, because you named the array (the variable) the same as the class.

I would also recommend to use a different style for class names and variables, e.g. class names with capital start (Myclassname) or camel-case (MyClassName). And all lower-case for variable names. This reduces confusion.

Code: Select all

Produkt products[50];
Use the source, Luke!
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 675
Joined: Tue Jul 26, 2016 2:00 pm

Re: Function of class doesn't work

Post by Wanderer82 »

Thanks for the recommendations. And also the hint that I only defined the variable with an index and not a "real" variable called "Produkt". I got it to work now.

Sometimes I change the names of variables etc. while I'm writing the code. I just use any name at the beginning just to play around and test.
Post Reply