Running various root codes with same canvas name in the same root session gives redefinition issue

I have a few programs which I run in the same root session, but I have observed that since they have same definition of canvas ie.

  auto c1 = new TCanvas("c1","Title",500,500);

I get the following error

  /home/file.c:3:9: error: redefinition of 'c1'
   auto c1 = new TCanvas("c1","Title",500,500);

I want to know any command which can be used to over-write the previos definition of canvas or a command which be used to delete a previously used canvas.

It is getting a little tedious to quit and again start new session for running each and every program, also I know I can use different definition of canvas for each program but I’ll be happy if there is another alternative.

Thank you!

ROOT platform : 6.14/06
OS : CentOS 6

ROOT is a C++ interpreter, and in C++ it’s not possible to redeclare a variable. That said, it might be possible to make the interpreter behave differently and allow overwrites if the type is the same. However, think about something like this:

double c = 3.0;
auto c = new TCanvas();

what should ROOT do? It’s not easy to solve this because code used later may think that c is a double.

I tried passing

command through root prompt and then run my first root program using

.x root1.c

I get the following error

 error: redefinition of 'c' with a different type: 'TCanvas *' vs 'double'

Then I added the declaration of canvas in my code itself like

double c = 2.0;
auto c = new TCanvas ("c", "Title",500,500);

I get the following error

error: redefinition of 'c'
error: member reference type 'double' is not a pointer
  c->Draw();
error: member reference type 'double' is not a pointer
  c->Update(); 

I am unsure of how to use

Sorry, what I meant is that in the future we may change the interpreter to accept redefinitions, but what you got is the expected behavior, that is, it’s not possible to redefine variables once they are declared. If you need to use the same name in your programs, try putting them on a local scope in each program, like this:

{
  double c = 0.0;
  std::cout << c << '\n';
}

{
  auto c = new TCanvas(); // now it works, as the other variable does not exist anymore
  // use c as a canvas here
}

As per your suggestion I did the following

{
  double c1 = 5.0;
  std::cout << c1 << '\n';
}

{
  c1= new TCanvas ("c1","Canvas",500,500);			//Manually create a Canvas with required Dimensions

//Plotting a graph using SetPoint command which enables you to feed in the data point by point.
  g= new TGraph();
  g->SetPoint(0,1,1);						//Manually set the point (point number, X value, Y value)
  g->SetPoint(1,5,25);
  g->SetPoint(2,10,100);
  g->SetPoint(3,20,400);
  g->SetTitle("Squares; number ; square");
  g-> Draw ("AC");

}

I get the following error message

7:1: error: expected unqualified-id

I am extremely Sorry, I think I am being very stupid while implementing your suggestions that is why errors keep coming.

Hi @Aqsa_Shaikh

I looks like you did not declare the canvas and the graph properly. You have to specify a type.

So for the canvas:

TCanvas *c1 = new TCanvas("c1", "Canvas", 500,500);

Same thing for the graph:

TGraph *g = new TGraph();

The “auto” in Amadio’s declaration is a c++ functionality, in this case the compiler figures out the type for you.

On top of that, the curly brackets in your code are usually used for functions. So you need to give a function name in that case and then you can use it. Maybe you can share your whole code and explain how you are using it?

Cheers,
Nikkie

1 Like

@Aqsa_Shaikh, @NikkieD is right. Alternatively, you can use the auto keyword to declare things as, e.g.

auto c1 = new TCanvas();
1 Like

Thank you guys, @NikkieD and @amadio