我在 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);
}
为什么?
答案 0 :(得分:5)
因为MainWindow* SetupMainWindow()
是全局/独立函数,而不是Controller
类的成员。您必须在 controller.cpp 中将成员函数声明为MainWindow* Controller::SetupMainWindow()
。