C++ requires a type specifier for all declarations

I create vector<TGraph*> graph and vector<TH1D*> his:

static vector<TGraph*> graph;
static vector<TH1D*> his;

graph[0] = new TGraph();
graph[1] = new TGraph();
graph[2] = new TGraph();
graph[3] = new TGraph();
graph[4] = new TGraph();
graph[5] = new TGraph();

his[0] = new TH1D("His0", "Result", 200, -10, 10);
his[1] = new TH1D("His1", "Result", 200, -10, 10);
his[2] = new TH1D("His2", "Result", 200, -10, 10);
his[3] = new TH1D("His3", "Result", 200, -10, 10);
his[4] = new TH1D("His4", "Result", 200, -10, 10);
his[5] = new TH1D("His5", "Result", 200, -10, 10);

and gey errors:

/home/slava/Program_Files/Root/macros/QuestOne.C:5:1: error: C++ requires a type specifier for all declarations
graph[0] = new TGraph();
^
/home/slava/Program_Files/Root/macros/QuestOne.C:6:1: error: C++ requires a type specifier for all declarations
graph[1] = new TGraph();
^
/home/slava/Program_Files/Root/macros/QuestOne.C:7:1: error: C++ requires a type specifier for all declarations
graph[2] = new TGraph();
^
/home/slava/Program_Files/Root/macros/QuestOne.C:8:1: error: C++ requires a type specifier for all declarations
graph[3] = new TGraph();
^
/home/slava/Program_Files/Root/macros/QuestOne.C:9:1: error: C++ requires a type specifier for all declarations
graph[4] = new TGraph();
^
/home/slava/Program_Files/Root/macros/QuestOne.C:10:1: error: C++ requires a type specifier for all declarations
graph[5] = new TGraph();
^
/home/slava/Program_Files/Root/macros/QuestOne.C:12:1: error: C++ requires a type specifier for all declarations
his[0] = new TH1D("His0", "Result", 200, -10, 10); 
^
/home/slava/Program_Files/Root/macros/QuestOne.C:13:1: error: C++ requires a type specifier for all declarations
his[1] = new TH1D("His1", "Result", 200, -10, 10);
^
/home/slava/Program_Files/Root/macros/QuestOne.C:14:1: error: C++ requires a type specifier for all declarations
his[2] = new TH1D("His2", "Result", 200, -10, 10);
^
/home/slava/Program_Files/Root/macros/QuestOne.C:15:1: error: C++ requires a type specifier for all declarations
his[3] = new TH1D("His3", "Result", 200, -10, 10);
^
/home/slava/Program_Files/Root/macros/QuestOne.C:16:1: error: C++ requires a type specifier for all declarations
his[4] = new TH1D("His4", "Result", 200, -10, 10);
^
/home/slava/Program_Files/Root/macros/QuestOne.C:17:1: error: C++ requires a type specifier for all declarations
his[5] = new TH1D("His5", "Result", 200, -10, 10);
^

Why do I get this?! I don’t understand!


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hi. When you write

you create an empty vector (you create something that can contain histograms but which has nothing in it (it has 0 elements if you declare it this way) so with e.g. his[5] you are trying to use a “memory slot” which doesn’t exist) . To use the vector you must first add objects to it with push_back and then you can use these objects and access them through his[i].
example:

#include <TH1D.h>  
   #include <iostream>
   #include<TRandom3.h>
   #include <vector>
   void vec(){


        static vector<TH1D*> his; 
        char titolo[20];
        for (int i=0; i<6;i++ ){
          sprintf( titolo," His%d ",i);
          TH1D *a=new TH1D (titolo, "Result", 200, -10, 10); 
          his.push_back (a);   }
        for (int i=0;i<100;i++){
          his[0]->Fill (gRandom->Rndm() );}
        his[0]->Draw();
       
       }
1 Like

Regarding the actual error you are getting: in C++ you cannot perform vector assignments at global scope: this is not valid C++, while this is and achieves the same thing.

Cheers,
Enrico

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.