c++ - 无法从 .cpp 访问 .h 中声明的私有(private)成员

标签 c++ qt private-members

我在controller.h中声明了一个私有(private)变量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);
}

为什么?

最佳答案

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

关于c++ - 无法从 .cpp 访问 .h 中声明的私有(private)成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32112316/

相关文章:

C++20 概念 : Which template specialization gets chosen when the template argument qualifies for multiple concepts?

python - 如何在 QLabel 中使用 QPainter

java - 如果我们可以使用访问器访问私有(private)数据成员,那么为什么我们不能访问私有(private)方法呢?

c++ - glDeleteTextures 和 glDeleteBuffers 如何工作?

c++ - 使用 CRTP 和多重继承消除冗余

c++ - qt组合框可读但可编辑

c++ - c++中隐藏特定函数的一种方法

java - 您可以使用 OGNL 访问私有(private)变量吗?

c++ - 手动设置 Visual Studio 2012 的 C++ 语言版本

c++ - 设置自定义图标