c++ - c++ 在非类函数中使用类变量
问题描述
我正面临这个新手问题。假设类MainFrame
(以下代码不会编译 - 我试图对我正在做的事情给出一个基本概念,因为我认为我的问题很容易被比我更有知识的人解决),它存在于文件gui.cxx
中与其他功能。请注意,这是一个更大项目的一部分,因此我将跳过包含的 main.cxx gui.h
。
在函数中start_gui_with_config()
,我尝试使用来自MainFrame
. 目前被宣布为private
所以我期待有一个 text_data_path was not declared in this scope
.
我还在类定义中声明了这个变量public
,但是在使用其中一个时我得到了同样的错误。static
gui.h
text_data_path ->SetText(data_path);
当我使用MainFrame::text_data_path ->SetText(data_path);
(仍然text_data_path
被声明为private
and static
)时,我 在构造函数(file )undefined reference to MainFrame::text_data_path
中使用的任何行中都会收到错误,奇怪的是,我每行都收到两次此错误。text_data_path
MainFrame::MainFrame
gui.cxx
最后,我尝试将所有函数(start_gui()
, start_gui_with_config()
)作为其中的一部分,MainFrame
并将它们声明为static void
(在这种情况下,我收到错误错误:无法声明成员函数 static void MainFrame::start_gui_with_config() 在 上具有静态链接)gui.cxx
或void
(在在这种情况下,我收到错误错误:无法在没有对象的情况下调用成员函数 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
解决方案
我建议你在课堂上使用二传手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
智能指针之外的问题。我建议确保您对动态分配感到满意,否则恐怕您会比预期更早地面临难以调试的错误
推荐阅读
- python - 为什么 pyautogui 的 write() 自己实现键延迟
- java - 我根据用户从 Excel 表中选择的浏览器并行进行跨浏览器测试的方法是否正确?
- java - 在 TestNG 中运行包时出现 java.lang.NullPointerException
- powershell - 如何过滤掉内置和不需要的组(Where-Object PS)
- python - 如何将文件夹中的所有文件作为输入(一次一个)并在处理后将它们保存到另一个文件夹中?
- go - 使用管道的 Redis 客户端计数
- javascript - 如何在 .test.js 中模拟 firebase .auth().currentUser
- c++ - 将 std::forward 用于未进一步传递给函数的通用引用是否有意义?
- javascript - cloneNode without losing event listener
- css - 对称形式