首页 > 解决方案 > c++ 在非类函数中使用类变量

问题描述

我正面临这个新手问题。假设类MainFrame(以下代码不会编译 - 我试图对我正在做的事情给出一个基本概念,因为我认为我的问题很容易被比我更有知识的人解决),它存在于文件gui.cxx中与其他功能。请注意,这是一个更大项目的一部分,因此我将跳过包含的 main.cxx gui.h

在函数中start_gui_with_config(),我尝试使用来自MainFrame. 目前被宣布为private所以我期待有一个 text_data_path was not declared in this scope.

我还在类定义中声明了这个变量public,但是在使用其中一个时我得到了同样的错误。staticgui.htext_data_path ->SetText(data_path);

当我使用MainFrame::text_data_path ->SetText(data_path);(仍然text_data_path被声明为privateand static)时,我 在构造函数(file )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.cxxvoid(在在这种情况下,我收到错误错误:无法在没有对象的情况下调用成员函数 void MainFrame::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()

gui.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

标签: c++classstatic

解决方案


我建议你在课堂上使用二传手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);

请注意,您的代码显然存在内存管理问题,并且作为一般规则,您永远不应该处理原始指针newdelete智能指针之外的问题。我建议确保您对动态分配感到满意,否则恐怕您会比预期更早地面临难以调试的错误


推荐阅读