String to TString

Hello,

I need to copy a string into a TString. Something like:

string line=“aaa”;
TString name;
name=line;

However, the last line is not valid. name.Data()=line does not work either. What would be the correct syntaxis?

Many thanks!

{
   string line1  = "aaa";
   printf ("line1 = %s\n",line1.c_str());
   TString line2 = line1.c_str();
   printf ("line2 = %s\n",line2.Data());
}

gives:

$  root string2tstring.C
   ------------------------------------------------------------------
  | Welcome to ROOT 6.37.01                        https://root.cern |
  | (c) 1995-2025, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for macosxarm64 on Sep 22 2025, 08:16:26                   |
  | From heads/master@v6-37-01-8233-g4254e9d6d43                     |
  | With Apple clang version 17.0.0 (clang-1700.3.19.1)              |
  | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q'  |
   ------------------------------------------------------------------

root [0] 
Processing string2tstring.C...
line1 = aaa
line2 = aaa

c_str() works fine. Thank you!

1 Like

It seems to me your ROOT distribution is broken.
The code snippet from your first post should work.
The “TString(const string& s)” constructor and the “TString& operator=(const string& s)” existed already in ROOT 5.34 (and probably also in some older versions).

You are right. I am using 5.28 binaries for windows with VS10, because that is the last stable combination that works under windows from my experience. I made an attempt recently to go to the latest version, but there are problems. See for instance:
ROOT on Windows 11 - ROOT - ROOT Forum

Best regards

Try to build the head of the v5-34-00-patches branch.