c++ 在非类函数中使用类变量
c++ Use a class variable in a non-class function
我正面临这个新手问题。假设类MainFrame
(以下代码不会编译 - 我试图给出我正在做什么的基本概念,因为我认为我的问题很容易由比我知识渊博的人解决(,它与其他函数一起存在于文件gui.cxx
中。请注意,这是一个更大项目的一部分,所以我跳过了我包含在gui.h
的main.cxx。
在函数start_gui_with_config()
中,我尝试使用来自MainFrame
的对象。目前被宣布为private
,所以我期待有一个text_data_path was not declared in this scope
.
我还在gui.h
年的类定义中将此变量声明为public
和static
,但随后在使用任一text_data_path ->SetText(data_path);
时出现相同的错误。
当我使用MainFrame::text_data_path ->SetText(data_path);
时(仍然text_data_path
被声明为private
和static
(,我在MainFrame::MainFrame
构造函数(文件gui.cxx
(中使用text_data_path
的任何行中都收到错误undefined reference to MainFrame::text_data_path
,奇怪的是,我每行都收到此错误两次。
最后,我尝试将所有函数(start_gui()
,start_gui_with_config()
(作为MainFrame
的一部分,并将它们声明为static void
(在这种情况下,我得到了一个错误错误:无法声明成员函数静态void MainFrame::start_gui_with_config((在gui.cxx
上具有静态链接(或void
(在这种情况下,我得到了错误错误: 不能调用成员函数 void 大型机::start_gui_with_config((main.cxx
上没有对象(。
关于如何在函数中使用text_data_path
的任何想法(即start_gui_with_config()
(不属于该类?
GUI.cxx
#include "../include/gui.h"
MainFrame::MainFrame(const TGWindow *p, UInt_t width, UInt_t height):TGMainFrame(p, width, height, kMainFrame|kHorizontalFrame){
// Define widgets
text_data_path = new TGTextEntry("/data/2020");
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This is a virtual constructor
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MainFrame::~MainFrame() {
// Clean up used widgets: frames, buttons, layout hints
Cleanup();
}//_____MainFrame::~MainFrame()
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This is to start the GUI with default settings
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void start_gui(){
// Popup the gui
std::cout << "Starting the gui" << std::endl;
new MainFrame(gClient->GetRoot(), 1000, 800);
}//_____start_gui()
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This is to start the GUI using the configuration file from previous session
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void start_gui_with_config(){
TString data_path = gSystem->GetFromPipe("awk '{if(NR==1) print $NF}' Config/last_session.cfg.viewer");
start_gui();
MainFrame::text_data_path->SetText(data_path);
}//____MainFrame::start_gui_with_config()
桂·
#ifndef ___GUI_H
#define ___GUI_H
//ROOT Includes
#include <TGTextEntry.h>
//C++ includes
using namespace std;
class MainFrame : public TGMainFrame {
private:
// Widgets
TGTextEntry *text_data_path;
public:
// Widgets
//static TGTextEntry *text_data_path;
MainFrame(const TGWindow *p, UInt_t width, UInt_t height);
virtual ~MainFrame();
//void start_gui_with_config();
//static void start_gui();
ClassDef (MainFrame,0);// Remove for ROOT6 and rootcling
};
void start_gui();
void start_gui_with_config();
#endif
我建议你在MainFrame
类上使用二传手:
void setDatapathText(TString const& newDatapath) {
text_data_path->SetText(data_path);
}
然后你可以在start_gui_with_config
函数中这样调用它:
auto frame = MainFrame(p, w, h);
frame.setDatapathText(data_path);
请注意,您的代码显然存在内存管理问题,作为一般规则,您永远不应该在智能指针之外处理原始new
和delete
。我建议您确保对动态分配感到满意,否则恐怕您将比预期更早地面临难以调试的错误
您的问题是您丢弃了指向MainFrame
的指针,因此在创建大型机后无法在start_gui_with_config()
中访问它。
解决此问题的一种方法是将void start_gui();
的签名更改为gui.h
标头中的MainFrame* start_gui();
。
在gui.cxx
将实现更改为
MainFrame* start_gui() {
return new MainFrame(gClient->GetRoot(), 1000, 800);
}
然后在您的start_gui_with_config()
中像这样使用指针:
void start_gui_with_config(){
TString data_path = gSystem->GetFromPipe("awk '{if(NR==1) print $NF}' Config/last_session.cfg.viewer");
MainFrame* frame = start_gui();
frame->text_data_path->SetText(data_path);
}//____MainFrame::start_gui_with_config()
此代码假定 MainFrame 对象自行销毁,否则代码将泄漏内存。我假设这种破坏发生在窗口关闭后。我已经看到其他像Qt
这样的GUI框架这样做。
- c++ 在非类函数中使用类变量
- 为什么从另一个构造函数内部调用C++构造函数不修改类变量?
- 可变参数模板类的构造函数无法接受变量参数
- 如何在构造函数中访问类变量以分配它们,而无需在C++中使用此指针
- 从静态成员函数访问私有非静态类变量 - C++
- 重写子类构造函数中的父类变量
- 调用函数时,私有类变量不会更改
- 如何使用类中的函数从 main 打印变量,帮助理解 OOP c++
- Google Mock:在目标类的构造函数中实例化的模拟私有变量成员
- 创建一个简单的类及其变量和函数不断返回" segmentation fault (core dumped)"、C++
- 使用线程销毁类析构函数中的shared_ptr成员变量
- 如果类没有任何成员变量,则通过临时对象调用类的成员函数的开销是多少?
- 指向类变量中函数的指针
- 作用域仅限于函数的类变量
- 在 android 上从 Java 调用C++类方法或函数,而无需在每次调用时重新创建类/变量
- 如何释放C++异常类析构函数中的变量
- 如何制作特定类的成员函数或变量(如 STL 容器)的别名
- C++多态类变量和函数
- 根据模板类变量启用函数
- 使用给定框架调用/引用另一个类C++变量或函数