无法从.cpp

时间:2015-08-20 07:42:01

标签: c++ qt private-members

我在 controller.h 中声明了一个私有变量MainWindow mainWindow; 但我无法从 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);
}

为什么?

1 个答案:

答案 0 :(得分:5)

因为MainWindow* SetupMainWindow()是全局/独立函数,而不是Controller类的成员。您必须在 controller.cpp 中将成员函数声明为MainWindow* Controller::SetupMainWindow()