How to concatenate string in ROOT,c++?

3.7k views Asked by At

I want to concatenate two string and I did in my program like String Filename = name+ "" + extension where extension is an integer value that I read just above this line and name is the path that is already defined.

But in ROOT I am getting error like Error: + illegal operator for pointer 1

What went wrong here? Is there any other method?

3

There are 3 answers

3
cdmh On

If extension is an integer, then convert it to a string first.

std::string Filename = name+ "" + std::to_string(extension);

+""+ does nothing, btw

0
nishantjr On

I'm going to go ahead and assume the 'name' is a char*.

Char const*  name = "john";
Char const* space = " ";

Here name and space are 2 pointers to character arrays.

When you add try to add these 2 together, the compiler tries to add the value of the 2 pointer together. This makes no sense to the compiler. You can obviously only add an offset to a pointer.

The solution to this is to make sure that one of the 2 things you are adding is a std::string and not 'c string'.

0
resistancefm On

The TString class in ROOT has a function called "Format" which you can use to concatenate strings the same way you format a print statement. Here is the documentation for the Format method: https://root.cern.ch/root/html/TString.html#TString:Format

and here is the documentation for how the formatting works http://www.cplusplus.com/reference/cstdio/printf/