Question

So i just spend around 4 hours of nightime tring to fix this error but i cant. Its wxw application in C++.

I have an app class where i declare my main window. Then, i declare my second window in my first window class. I want to refer in my second window class to a class of the first window. I just keep getting this error.

E:\WORKSPACES\CodeBlocks\Studentonator\AddMarkOne.h|35|error: 'StudentonatorDialog' has not been declared|

My App class header file:

#ifndef STUDENTONATORAPP_H
#define STUDENTONATORAPP_H

#include <wx/app.h>
#include <wx/dialog.h>
#include <AddMarkOne.h>
#include <StudentonatorMain.h>

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

#endif // STUDENTONATORAPP_H

My main wxDialog header file:

#ifndef STUDENTONATORMAIN_H
#define STUDENTONATORMAIN_H

#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif

#include "AddMarkOne.h"
#include "StudentonatorApp.h"
#include "student.h"


#include <wx/button.h>
#include <wx/statline.h>
#include <wx/grid.h>
#include <list>
#include <iostream>
#include <fstream>
using namespace std;

class StudentonatorDialog: public wxDialog
{
    public:
        StudentonatorDialog(wxDialog *dlg, const wxString& title);
        void refresh();
        ~StudentonatorDialog();
        int ID_counter = 0;
        list<student*> GetList(){return StudentList;};
    protected:
        enum
        {
            idBtnAbout,
            idBtnAddStudent,
            idBtnRemoveStudent,
            idBtnEditStudent,
            idBtnAddMark
        };
        wxStaticText* m_staticText1;
        ...
        list<student*> StudentList;
        AddMarkOne* dlgaddmark1;

    private:
        void OnClose(wxCloseEvent& event);
        ...
        int numstrlevelcounter=0;
};

#endif // STUDENTONATORMAIN_H

And heres my second wxDialog class:

#ifndef ADDMARKONE_H
#define ADDMARKONE_H

#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif

#include <wx/dialog.h>
#include <wx/button.h>
#include <wx/statline.h>
#include <wx/grid.h>
#include <list>
#include <iostream>
#include <fstream>

#include "StudentonatorMain.h"
#include "student.h"
#include "StudentonatorApp.h"


using namespace std;

class AddMarkOne: public wxDialog
{
    public:
        AddMarkOne(wxDialog* dlg1, const wxString& title, StudentonatorDialog* maindialog);
        ~AddMarkOne();
        int ID_counter = 0;
        void SetID(int ID){this->ID = ID;}


    protected:
        enum
        {
            idBtnNext,
        };
        wxStaticText* m_staticText1;
        wxStaticLine* m_staticline1;
        wxButton* BtnNext;


    private:
        void OnClose(wxCloseEvent& event);
        int i;
        int j;
        DECLARE_EVENT_TABLE()
        int ID;

};

#endif // ADDMARKONE_H

please help :)

Was it helpful?

Solution

As my knowledge about C++ you can not assign a value to a class member in class declaration ( like Java language), you should do this in constructor

int ID_counter = 0;

and

 int numstrlevelcounter=0;

UPDATE:
you've declared class AddMarkOne which refers to StudentonatorDialog and declare class StudentonatorDialog which refers to AddMarkOne. It seems there is a loop here. and with this loop you can not be able to solve this problem. My suggestion is to refine your main architecture, inheritance and polymorphism

Your #include statements must have this pattern:
1. First more abstract class include statement then inherited class include statements
2. In your more abstract class you should not refer to inherited classes.
3. Your inheritance chain must be some thing like a tree (which is without any loops) as your include statements

OTHER TIPS

As others have noted, your problem is probably due to recursively including headers in all directions. While in this concrete example this could be avoided by just removing the unnecessary includes, a better idea in general in C++ is to use forward declarations: they are sufficient as long as you only declare methods using pointers or references to a class and avoid unnecessary compilation dependencies (i.e. the fact that all the code including "AddMarkOne.h" would currently need to be recompiled if "StudenatorMain.h" changes even if though it's completely unneeded).

So instead of #include "StudentonatorMain.h" just use a forward declaration class StudentonatorDialog;.

This the problem in your solution that headers file are preprocessed before compiled and you are declaring in it which are not compiled so you must make theStudentonatormain.h file must be complied so change the extension from .h to .cpp and recompiled than it will overcome the error

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