简体   繁体   中英

Can't access private member declared in .h from .cpp

I've declared a private variable MainWindow mainWindow; in controller.h but I cannot access it from controller.cpp

controller.h

#ifndef CONTROLLER_H
#define CONTROLLER_H

#include <QMainWindow>
#include "event_manager.h"
#include "event_wrapper.h"
#include "event_list_viewer.h"
#include "main_window.h"

class Controller
{
public:
    Controller();
    ~Controller();

    static Controller &GetInstance();
    MainWindow *SetupMainWindow();

private:
    MainWindow mainWindow;
};

#endif // CONTROLLER_H

controller.cpp

#include "controller.h"

Controller::Controller() {}
Controller::~Controller() {}

Controller& Controller::GetInstance()
{
   static Controller instance;
   return instance;
}

MainWindow* SetupMainWindow()
{
    bbque::EventManager *manager = &bbque::EventManager::GetInstance();
    bbque::EventWrapper *wrapper = manager->Deserialize(); //get wrapper via manager
    EventListViewer eventViewer;
    wrapper->setParent(&eventViewer);
    eventViewer = EventListViewer(wrapper, mainWindow); //cannot access mainWindow variable
    return new MainWindow(eventViewer);
}

Why?

Because MainWindow* SetupMainWindow() is a global/standalone function, not a member of your Controller class. You must declare the member function as MainWindow* Controller::SetupMainWindow() in controller.cpp .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM