c ++在非类函数中使用类变量

塔诺斯

我正在面对这个新手问题。假设类MainFrame(下面的代码不会编译-我想给我在做什么,因为我觉得我的问题很容易被人比我更了解解决一个基本的想法),它生活在文件gui.cxx一起与其他功能。请注意,这是一个较大项目的一部分,因此我将跳过其中已包含的main.cxx gui.h

在函数中,start_gui_with_config()我尝试使用中的对象MainFrame目前已声明为,private因此我希望有一个text_data_path was not declared in this scope

我也在类定义中publicstatic将该变量声明为gui.h但是在使用其中任何一个时,我都遇到相同的错误text_data_path ->SetText(data_path);

当我使用时MainFrame::text_data_path ->SetText(data_path);(仍然text_data_path声明为privatestatic),我构造函数(文件)中undefined reference to MainFrame::text_data_path使用的任何行均出现错误,奇怪的是,每行两次都出现此错误。text_data_pathMainFrame::MainFramegui.cxx

最后,我尝试将所有函数(start_gui()start_gui_with_config())的一部分MainFrame声明为static void(在这种情况下,我得到一个错误错误:无法声明成员函数static void MainFrame :: start_gui_with_config()gui.cxx)或void(in在这种情况下,我得到了错误错误:无法没有对象的情况下调用成员函数void MainFrame :: start_gui_with_config(main.cxx)。

关于如何 text_data_pathstart_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()

h

#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因此在start_gui_with_config()创建MainFrame之后就无法访问它

解决这个问题的方法之一是改变的签名void start_gui();,以MainFrame* start_gui();你的gui.h头。

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对象将自行销毁,否则该代码将泄漏内存。我认为这种破坏是在窗户关闭后发生的。我已经看到其他类似的GUI框架Qt

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章