String::erase and ROOT

I am puzzled about the use of the string class in ROOT.

Following is a minimal example which illustrates the situation:

#include <iostream>

int test() {
  std::string str = " Some test string ";

  std::cout << str << std::endl;
  for (int i=0; i<str.length(); i++) {
    if (str[i]==' ')
      str.erase(i,1);
  }
  std::cout << str << std::endl;
  return 0;
}

Upon execution from within ROOT (i.e. not as compiled code), the code fails to execute with the error:

Could somebody explain to me why this does not work. I am trying to use some classes within ROOT that use various string manipulations such as erase, and I would like to use those codes as scripts from within ROOT, i.e. as

.L some_script.cxx some_script_function()

Cheers,
Maurits

Hi Mauritz,

Your example code works fine for me - the output I get is

[quote]root [1] .x test.C
Some test string
Someteststring
(int)0
[/quote]

Are you using your own build or one of the ones on nucserver4?

Regards

I’m using version 5.27/04.

That’s odd. If I do

.L test.cxx
test()

it terminates with afore-mentioned error

Error: Can't call string::erase(i,1) in current scope test.cxx

The same happens if I do

.x test.cxx

Maurits

PS. BTW, same error with version on nucserver4.

Hi Mauritz,

I am using ROOT 5.29/03 (trunk@39055 on linuxx8664gcc).

For me your code runs fine both as originally written and with a minor modification so that I can pass it an arbitrary string.

[code]#include

void test(string str) { //<------ change so that I can pass it an arbitrary string
//std::string str = " Some test string ";

std::cout << str << std::endl;
for (int i=0; i<str.length(); i++) {
if (str[i]==’ ')
str.erase(i,1);
}
std::cout << str << std::endl;
//return 0;
}
[/code]
The response I get is as follows

[quote]root [0] .L test.cxx
root [1] test(“This is a test”)
This is a test
Thisisatest
root [2]
[/quote]

After installing ROOT version 5.28/00c the problem persists. I would prefer not having to install the RC version.

Error: Can't call string::erase(i,1) in current scope test.cxx:9:
Possible candidates are...
*** Interpreter error recovered ***

I am at a loss.

Hi Mauritz,

Unfortunately 5.28/00c won’t build on my Ubuntu 11.04 system (due to a libX11 problem) so I can’t see if I get the same error when I run the same version as you do.

Sorry

UPDATE

So after installing the latest RC version 5.30/00-rc2 the error has disappeared and the script executes as expected. I am still puzzled (and mildly annoyed) as to why this is.

If anybody could shed some light onto this, I would greatly appreciate it. To briefly repeat and summarize, the minimal sample code in the first posting of this thread terminates with an error about the string::erase function, when executed within ROOT versions 5.27 and 5.28. This is a problem, as not everybody likes to run the development version and string manipulations using the C++ string class are frequently required.

Maurits

I had the same problem with str::erase() on 5.26 version.
I used str::substr() - which executes without problem and was suitable for my purposes.