Confusion when declaring a TFile

I know this is a completely trivial question, but I am confused about how to declare a TFile without the use of pointers and would appreciate any advice.

Why does the following compile:

#include <iostream>
#include <string>
#include "TFile.h" 
int main(){
	const std::string filename = "output.root";
	const unsigned int compression = 1;
	TFile t1(filename.c_str(), "Recreate", "title", compression);
}

but not this: ?

#include <iostream>
#include <string>
#include "TFile.h" 
class MyClass{
private:
	const std::string filename = "output.root";
	const unsigned int compression = 1;
	TFile t2(filename.c_str(), "Recreate", "title", compression);
};

int main(){
	MyClass a;
}

With the latter, the error I get is

test.cpp:11:11: error: ‘filename’ is not a type                                                                                
   11 |  TFile t2(filename.c_str(), "Recreate", "title", compression);                                                              
      |           ^~~~~~~~                                                                                                          
test.cpp:11:19: error: expected ‘,’ or ‘...’ before ‘.’ token                                                                  
   11 |  TFile t2(filename.c_str(), "Recreate", "title", compression);                                                              
      |    

Many thanks for any help.


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.20
Platform: Linux
Compiler: g++ v9.3.0


Hi @ed20

I think constructor like this TFile t1("file.root", "RECREATE");
doesn’t work inside the class, because it misidentifies it as a method.

But you can easily do:
TFile t1 = TFile("file.root", "RECREATE")

I am not an expert, but I also stumbled on this error and that’s my understanding of it.

Cheers,
Bohdan

Aha, that makes sense, thanks. Unfortunately your suggested method (which was what I tried first of all) does not seem to work from within a class either.

class MyClass{
private:
	const std::string filename = "output.root";
	const unsigned int compression = 1;
	TFile t2 = TFile(filename.c_str(), "Recreate", "title", compression);
};

int main(){
	MyClass a;
}

results in the error

test.cpp:11:69: error: use of deleted function ‘TFile::TFile(const TFile&)’                                                    
   11 |  TFile t2 = TFile(filename.c_str(), "Recreate", "title", compression);                                                      
      |                                                                     ^                                                       
In file included from test.cpp:3:                                                                                              
/usr/local/bin/root_v6.20.02/include/TFile.h:167:4: note: declared here                                                             
  167 |    TFile(const TFile &) = delete;            //Files cannot be copied                                                       
      |    ^~~~~     

Perhaps the only way is to make t2 a global quantity or a pointer.

Ah, sorry,

What about this?

TFile file;
file.Open("file.root", "RECREATE");

Here. Sorry for posting before checking

cheers

This works in the main body of the code , but again fails if I want the TFile to be a member of a class, with the error “'file' does not name a type”. This is my my minimal code:

#include "TFile.h"

class MyClass{
	TFile file;
	file.Open("file.root", "RECREATE");
};

int main(){
	MyClass a;
}

which fails to compile. It seems to compile with

TFile file = {"file.root", "RECREATE"};

so that it is not being confused for a method, but this is at best nonstandard and I have never seen TFiles declared in this way, so I’m not sure if this is legitimate.

Hello,

What about placing the creation of the TFile object in a constructor of your class?

OK, thanks, I’ll try this too