Probleme with vector of structure

Hello everybody,

I am trying to create a vector of strucutre but it seems it is not working and i do not understand why.

#include <vector> typedef struct test{ int id, type, number; } ; void main(){ .... vector <test> v; .... }

This is the syntax i use.
It is as if Cint did not recognized my vector.
I have many mistakes which disapear when i delete "vector < test> v;

This is the error message

[code]
Note: Link requested for already precompiled class vector<double,allocator > (ignore this message) :0:
In file included from /home/homelocal/GeomRoo/AutoDict_vector_test_.cxx:2,
from /home/homelocal/GeomRoo/AutoDict_vector_test__cxx_ACLiC_dict.h:34,
from /home/homelocal/GeomRoo/AutoDict_vector_test__cxx_ACLiC_dict.cxx:17:
/home/homelocal/GeomRoo/./AssemblageThomas.C:28:18: warning: extra tokens at end of #include directive
In file included from /home/homelocal/GeomRoo/AutoDict_vector_test_.cxx:2,
from /home/homelocal/GeomRoo/AutoDict_vector_test__cxx_ACLiC_dict.h:34,
from /home/homelocal/GeomRoo/AutoDict_vector_test__cxx_ACLiC_dict.cxx:17:
/home/homelocal/GeomRoo/./AssemblageThomas.C:40: warning: ‘typedef’ was ignored in this declaration
/home/homelocal/GeomRoo/./AssemblageThomas.C:43: error: expected constructor, destructor, or type conversion before ‘->’ token

Error in : Compilation failed!
In file included from /home/homelocal/GeomRoo/AutoDict_random_access_iterator_test_long_.cxx:2,
from /tmp/rootcint_V45J9B.h:3,
from /tmp/BZehJI_cint.cxx:1:
/home/homelocal/GeomRoo/./AssemblageThomas.C:28:18: warning: extra tokens at end of #include directive
Error in : Dictionary generation failed!
In file included from AutoDict_random_access_iterator_test_long_.cxx:2:
/home/homelocal/GeomRoo/./AssemblageThomas.C:28:18: warning: extra tokens at end of #include directive[/code]

Could you please help me ?
Thank you.

[code]#include

typedef struct test { // add the “test” name into the struct/union/enum tag namespace
int id, type, number;
} test; // add the “test” name into the typedef namespace

void trial(void) { // don’t call it “main” unless it’s a standalone application
//…
std::vector v;
//…
}[/code]

Thank you for your answer but if i want to put the vector in the main() whose name is in my case "assemblagethomas()"how can i do ?

Simply replace my “trial” with your “assemblagethomas”.

I did it:

[code]
#include

typedef struct test {
int id, type, number;
} test;

void AssemblageThomas() {
//…
std::vector v;
//…
}[/code]

and there is the same error.

What means “[quote]add the “test” name into the struct/union/enum tag namespace[/quote]” ??

Then try: [code]#include

typedef struct { // an anonymous structure
int id, type, number;
} test; // add the “test” name into the typedef namespace

void AssemblageThomas() {
//…
std::vector v;
//…
}[/code] and/or: [code]#include

struct test { // add the “test” name into the struct/union/enum tag namespace
int id, type, number;
};

void AssemblageThomas() {
//…
std::vector v;
//…
}[/code]

Unfortunately, that did not solve the problem.
I think that comes from vector. I did not precise: my root version is 5.30. Maybe vector of structure is not implemented in this version ?

Maybe try to replace “struct” with “class” (should be exactly the same, but you might need to add “public:” inside).
I see no problems with ROOT 5.30/05.

There is the same problem…
Should i add something to make it work in the #ifdef __MAKECINT __ /// #endif ?

I have no idea what happens. Let’s wait for Philippe.
In the meantime, maybe you should have a careful look at:
/home/homelocal/GeomRoo/./AssemblageThomas.C:28:18: warning: extra tokens at end of #include directive

I built a litle part of code just for a test (because my previous programme is about 1600 lines).

[code]
#include

struct test2{
int id, type, number;
} ;

void test() {
gSystem->Load(“libGeom”);
TGeoManager *geom = new TGeoManager(“test_conditions”, “Boundary Conditions Test”);
vector v;
v.push_back(test2());
v.back().id = 3;
v.back().type = 5;
v.back().number = 8;

TGeoMaterial *vacuum = new TGeoMaterial(“vacuum”, geom->GetElementTable()->GetElement(0), 1);
TGeoMedium *Sodium = new TGeoMedium(“Sodium”, 1,vacuum);

TGeoPgon *hexaW = new TGeoPgon(“W”, 0., 360. , 6 , 2) ;
hexaW-> DefineSection(0, -10/2, 0, 20) ;
hexaW-> DefineSection(1, 10/2,0, 20) ;
TGeoVolume *world = new TGeoVolume(“world”,hexaW,Sodium);

geom->SetTopVolume(world);
geom->CloseGeometry();
world->SetVisContainers(kTRUE);
geom->SetTopVisible(kTRUE);
world->Draw();

} [/code]

If you put this in root, do you have the same problem ?

Add: #include "TSystem.h" #include "TGeoManager.h" #include "TGeoMaterial.h" #include "TGeoMedium.h" #include "TGeoVolume.h" #include "TGeoPgon.h"
Then I get: root [0] .x test.cxx Info in <TGeoManager::TGeoManager>: Geometry test_conditions, Boundary Conditions Test created Info in <TGeoManager::SetTopVolume>: Top volume is world. Master volume is world Info in <TGeoManager::CheckGeometry>: Fixing runtime shapes... Info in <TGeoManager::CheckGeometry>: ...Nothing to fix Info in <TGeoManager::CloseGeometry>: Counting nodes... Info in <TGeoManager::Voxelize>: Voxelizing... Info in <TGeoManager::CloseGeometry>: Building cache... Info in <TGeoNavigator::BuildCache>: --- Maximum geometry depth set to 100 Info in <TGeoManager::CloseGeometry>: 1 nodes/ 1 volume UID's in Boundary Conditions Test Info in <TGeoManager::CloseGeometry>: ----------------modeler ready---------------- Info in <TCanvas::MakeDefCanvas>: created default TCanvas with name c1

I think it is now working for me…
I do not understand!
Without this vector, all is perfectly working. With this vector i need to add TGeoMedium, TGeovolume …etc

In case of “magic” problems, always try to precompile your macro with ACLiC (i.e. “.L test.cxx++”) -> this will help to reveal the majority of problems.
In the “current” case … when building the dictionary (“AutoDict_*”) for the “test” vector, the whole “test.cxx” file was being #included, because that’s where the “test” structure was defined, and then the compiler was failing (so one needed to add all required includes).

I thank you for your help. Is it possible to know all the include who are missing ?
Because in my real programme, other includes are missing

Thomas

Typically, for every ROOT class that you use in your source code, there should be a corresponding #include (usually with the same name as the class itself plus “.h”).
Also, watch the error messages from the compiler (e.g. when called by ACLiC) which will complain about:
… error: ‘SomeVariable’ was not declared in this scope
… error: expected type-specifier before ‘SomeClass’
… error: expected ‘;’ before ‘SomeAnotherClass’
Then you typically need to:
#include “ClassOfSomeVariable.h” // for “SomeVariable”
#include “SomeClass.h”
#include “SomeAnotherClass.h”

[quote]Without this vector, all is perfectly working. With this vector i need to add TGeoMedium, TGeovolume …etc[/quote]CINT has trouble with interpreting std::vectors, so we switch to automatically compile the templates whenever we can. In your case this means that once you add a vector, the script in which your declared the type of the content a the vector will be compiled (instead of being interpreted). This compilation in turn requires that the script compiles strictly with the C++ standard.

Cheers,
Philippe.