Array of TGraphErrors


I want to create an array of TGraphErrors.
Is it possible and how?

int n = 50; TMultiGraph *mg = new TMultiGraph(); TGraphErrors *gr[n]; for( n = 0; n < i; n++ ) { *gr[n] = new TGraphErrors( 1, &redshift[n], &gamma[n], &eredshift[n], &egamma[n] ); gr[n]->SetMarkerColor(n+2); gr[n]->SetLineColor(n+2); gr[n]->SetMarkerStyle(20); mg->Add( gr[n] ); }

This code gives me errors:
plot_sources.cpp:58:14: error: no viable overloaded ‘=’
*gr[n] = new TGraphErrors( 1, &redshift[n], &gamma[n], &eredshift[n], &egamma[n] );
~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/root/root_v5.34/include/TGraphErrors.h:55:18: note: candidate function not viable: no
known conversion from ‘TGraphErrors *’ to ‘const TGraphErrors’ for 1st argument; dereference
the argument with *
TGraphErrors& operator=(const TGraphErrors &gr);
1 error generated.

Your code contains C++ errors, which are not related to ROOT.

In cases like this one, a solution is to allocate and deallocate the array with something like

myClass* newArray = new MyClass[32];
delete[] newArray;

Shouldn’t it be the same identifier on both sides? I mean myClass and MyClass?

Let’s put it like this then

auto newArray = new MyClass[32];
delete[] newArray;

Where i was defined or at least declared?


good point… I kind of missed that.