title: c++ Use a class variable in a non-class function
body: <p>I'm facing this novice issue. Assume the class <code>MainFrame</code> (the following code isn't going to compile - I'm trying to give a basic idea of what I'm doing because I think my problem is easy to solve by someone more knowledgeable than me) which lives on file <code>gui.cxx</code> along with other functions. Note that this is part of a larger project so I'm skipping the main.cxx which I have included <code>gui.h</code>.</p>
  <p>In the function <code>start_gui_with_config()</code> I'm trying to use an object from <code>MainFrame</code>. At the moment is declared as <code>private</code> so I'm expecting to have an  <code>text_data_path was not declared in this scope</code>.</p>
  <p>I also declared this variable as <code>public</code> and <code>static</code> in the class definition in <code>gui.h</code> but then I get the same error when using either <code>text_data_path  -&gt;SetText(data_path);</code>.</p>
  <p>When I'm using <code>MainFrame::text_data_path  -&gt;SetText(data_path);</code> (still <code>text_data_path</code> is declared as <code>private</code> and <code>static</code>) I get the error  <code>undefined reference to MainFrame::text_data_path</code> in any line I'm using <code>text_data_path</code> within the <code>MainFrame::MainFrame</code> constructor (file <code>gui.cxx</code>) and strangely I get this error twice for each line.</p>
  <p>Finally I tried making all the functions (<code>start_gui()</code>, <code>start_gui_with_config()</code>) part of <code>MainFrame</code> and I declared them as either <code>static void</code> (in this case I got an error <em>error: cannot declare member function static void MainFrame::start_gui_with_config() to have static linkage</em> on the <code>gui.cxx</code> ) or <code>void</code> (in this case I got the error <em>error: cannot call member function void MainFrame::start_gui_with_config() without object</em> on the <code>main.cxx</code>).</p>
  <p>Any idea on how to use <code> text_data_path</code> in a function (i.e. <code>start_gui_with_config()</code>) that doesn't belong to the class?</p>
  <p><strong>gui.cxx</strong></p>
  <pre><code>#include &quot;../include/gui.h&quot;    
  
  MainFrame::MainFrame(const TGWindow *p, UInt_t width, UInt_t height):TGMainFrame(p, width, height, kMainFrame|kHorizontalFrame){
  
  // Define widgets
  text_data_path = new TGTextEntry(&quot;/data/2020&quot;);
  
  }
  
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  // 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 &lt;&lt; &quot;Starting the gui&quot; &lt;&lt; std::endl;
     new MainFrame(gClient-&gt;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-&gt;GetFromPipe(&quot;awk '{if(NR==1) print $NF}' Config/last_session.cfg.viewer&quot;);
      
      start_gui();
      
      MainFrame::text_data_path-&gt;SetText(data_path);
      
  }//____MainFrame::start_gui_with_config()
  </code></pre>
  <p><strong>gui.h</strong></p>
  <pre><code>#ifndef ___GUI_H
  #define ___GUI_H
  
  //ROOT Includes
  #include &lt;TGTextEntry.h&gt;
  
  //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
  </code></pre>