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