# Ambiguous expansion of macro 'ClassDef'

**URL:** <https://root-forum.cern.ch/t/ambiguous-expansion-of-macro-classdef/61958>\
**Category:** Newbie\
**Created:** [October 18, 2024, 6:34am UTC](https://root-forum.cern.ch/t/ambiguous-expansion-of-macro-classdef/61958 "2024-10-18T06:34:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![amansas](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/3ec8ea/32.png) [@amansas](https://root-forum.cern.ch/u/amansas)\
**Post date:** [October 18, 2024, 6:34am UTC](https://root-forum.cern.ch/t/ambiguous-expansion-of-macro-classdef/61958/1 "2024-10-18T06:34:20Z")

</div>

* * *

_ROOT Version:_ 6.32.06  
_Platform:_ Ubuntu 22.04  
_Compiler:_ Not Provided

* * *

Hello all,  
I’m following a tutorial provided by my prof to make my own EventClass. The header file is as follows:

```auto
#ifndef _MYEVENT_
#define _MYEVENT_
#include <iostream>
#include <vector>
#include "TObject.h"

class MyEvent{
 public:
  MyEvent(){ px = 0; py = 0; pt = 0;};
  virtual ~MyEvent(){};

  void setPx(float x_){ px = x_;};
  void setPy(float y_){ py = y_;};
  void setPt(float t_){ pt = t_;};

  float getPx(){return px;};
  float getPy(){return py;};
  float getPt(){return pt;};

 private:
  float px;
  float py;
  float pt;
  ClassDef(MyEvent,1)
};
#endif

```

I then go to my linux terminal and input:  
`root -l`  
`.L MyEvent.h+`  
`.L tree_example8.C`

but i encounter a warning here, given by:

```auto
/home/amensaywho/ROOT_Tutorial/MyEvent.h:25:3: warning: ambiguous expansion of macro 'ClassDef' [-Wambiguous-macro]
  ClassDef(MyEvent,1)
  ^
/home/amensaywho/root/include/Rtypes.h:337:9: note: expanding this definition of 'ClassDef'
#define ClassDef(name,id) \
        ^
input_line_4:22:9: note: other definition of 'ClassDef'
#define ClassDef(name, id) \
        ^

```

the code for tree\_example8.C is:

```auto
//Example of Tree using MyEvent Class 
#include "MyEvent.h"

void tree_example8() {

  TFile *f = new TFile("myfile_example8.root", "RECREATE");
  TTree *T = new TTree("T","tree with class");
  TRandom r;
  Float_t px,py,pt;

  MyEvent *event = new MyEvent();
  T->Branch("Event", "MyEvent", &event);

  for (Int_t i = 0; i < 10000; i++) {
    r.Rannor(px,py);
    pt = std::sqrt(px*px + py*py);
    
    event->setPx(px);
    event->setPy(py);
    event->setPt(pt);

    T->Fill();
  }
  
  f->Write();
  f->Close();
}

```

Can someone point out what I’m missing? I’ve read the manual and documentation but I’m kinda new to programming in general so the jargon is a bit beyond me…

---

<div class="post-metadata">

**Author:** ![mdessole](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mdessole/32/34021_2.png) [@mdessole](https://root-forum.cern.ch/u/mdessole)\
**Post date:** [October 18, 2024, 7:13am UTC](https://root-forum.cern.ch/t/ambiguous-expansion-of-macro-classdef/61958/2 "2024-10-18T07:13:47Z")

</div>

Hello @amansas,

welcome to the ROOT forum!  
In order to execute your macro you should run  
`.x tree_example8.C`  
I tried your code and it runs fine for me. Could you please retry executing with `.x`?

Monica

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [October 18, 2024, 9:20am UTC](https://root-forum.cern.ch/t/ambiguous-expansion-of-macro-classdef/61958/3 "2024-10-18T09:20:14Z")

</div>

I can not reproduce the problem with the code you provided. So there might be something odd with your ROOT installation. Let check a few things. On the shell prompt execute this following commands and give the output:

```auto
echo $ROOTSYS
which root.exe
g++ --version
cat $ROOTSYS/include/compiledata.h
ldd `which root.exe`
echo $PATH
echo $LD_LIBRARY_PATH

```

---

<div class="post-metadata">

**Author:** ![amansas](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/3ec8ea/32.png) [@amansas](https://root-forum.cern.ch/u/amansas)\
**Post date:** [October 18, 2024, 9:23am UTC](https://root-forum.cern.ch/t/ambiguous-expansion-of-macro-classdef/61958/4 "2024-10-18T09:23:27Z")

</div>

Ah ok, `.x` works for me, thank you. The steps we were asked to follow were:  
` .L MyEvent.h+`  
`.L MyEvent_h.so`  
`.L tree_example8.C`  
`tree_example8()`

Aren’t the last 2 steps equivalent to running `.x tree_example8.C` ? My warning pops up after `.L tree_example8.C`

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [October 18, 2024, 9:27am UTC](https://root-forum.cern.ch/t/ambiguous-expansion-of-macro-classdef/61958/5 "2024-10-18T09:27:07Z")

</div>

```auto
.L MyEvent.h+
.L MyEvent_h.so

```

The `.L ...so` is redundant and is supposed to have been done already by the `.L ...+`

```auto
.L tree_example8.C
tree_example8()

```

This is meant to do _exactly_ the same as

```auto
.x tree_example8.C

```

---

<div class="post-metadata">

**Author:** ![amansas](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/3ec8ea/32.png) [@amansas](https://root-forum.cern.ch/u/amansas)\
**Post date:** [October 18, 2024, 9:38am UTC](https://root-forum.cern.ch/t/ambiguous-expansion-of-macro-classdef/61958/6 "2024-10-18T09:38:51Z")

</div>

I see. We were asked to run `.L MyEvent_h.so` everytime we quit root and wanted to run a macro that used the Event Class defined by MyEvent.h. Is that redundant as well?

Info for my ROOT Installation:

```auto
amensaywho@CONSOLE:~/ROOT_Tutorial$ echo $ROOTSYS
/home/amensaywho/root
amensaywho@CONSOLE:~/ROOT_Tutorial$ which root.exe
/home/amensaywho/root/bin/root.exe
amensaywho@CONSOLE:~/ROOT_Tutorial$ g++ --version
g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

amensaywho@CONSOLE:~/ROOT_Tutorial$ cat $ROOTSYS/include/compiledata.h
/* This file is automatically generated */
#define BUILD_ARCH "linuxx8664gcc"
#define BUILD_NODE "Linux 4206edb925e4 4.18.0-553.16.1.el8_10.x86_64 #1 SMP Thu Aug 8 07:11:46 EDT 2024 x86_64 x86_64 x86_64 GNU/Linux"
#define CXX "c++"
#define COMPILER "/usr/bin/c++"
#define COMPILERVERS "gcc1140"
#define COMPILERVERSSTR "c++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
#define MAKESHAREDLIB "cd $BuildDir ; c++ -fPIC -c $Opt -Wno-implicit-fallthrough -Wno-noexcept-type -pipe -W -Woverloaded-virtual -fsigned-char -pthread -std=c++17 $IncludePath $SourceFiles ; c++ $Opt $ObjectFiles -shared -rdynamic $LinkedLibs -o $SharedLib"
#define MAKEEXE "cd $BuildDir ; c++ -c $Opt -Wno-implicit-fallthrough -Wno-noexcept-type -pipe -W -Woverloaded-virtual -fsigned-char -pthread -std=c++17 $IncludePath $SourceFiles; c++ $Opt $ObjectFiles -rdynamic -o $ExeName $LinkedLibs"
#define CXXOPT "-O3 -DNDEBUG"
#define CXXDEBUG "-g"
#define ROOTBUILD ""
#define LINKEDLIBS "-L$ROOTSYS/lib -lCore -lRint "
#define INCLUDEPATH "-I$ROOTSYS/include"
#define OBJEXT "o"
#define SOEXT "so"
amensaywho@CONSOLE:~/ROOT_Tutorial$ ldd `which root.exe`
        linux-vdso.so.1 (0x00007ffc2a590000)
        libRint.so => /home/amensaywho/root/lib/libRint.so (0x00007effb9eda000)
        libCore.so => /home/amensaywho/root/lib/libCore.so (0x00007effb9a23000)
        libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007effb97ef000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007effb97cf000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007effb95a6000)
        /lib64/ld-linux-x86-64.so.2 (0x00007effb9f0f000)
        libpcre2-8.so.0 => /lib/x86_64-linux-gnu/libpcre2-8.so.0 (0x00007effb950d000)
        libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007effb94f1000)
        liblzma.so.5 => /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007effb94c6000)
        libxxhash.so.0 => /lib/x86_64-linux-gnu/libxxhash.so.0 (0x00007effb94b1000)
        liblz4.so.1 => /lib/x86_64-linux-gnu/liblz4.so.1 (0x00007effb9491000)
        libzstd.so.1 => /lib/x86_64-linux-gnu/libzstd.so.1 (0x00007effb93c2000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007effb92d9000)
amensaywho@CONSOLE:~/ROOT_Tutorial$ echo $PATH
/home/amensaywho/geant4/geant4-v11.2.2-install/bin:/home/amensaywho/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/Cloudflare/Cloudflare WARP/:/mnt/c/Program Files/Python/Python311/Scripts/:/mnt/c/Program Files/Python/Python311/:/mnt/c/Users/Amen/AppData/Local/Microsoft/WindowsApps:/mnt/c/Program Files/Microsoft VS Code/bin:/snap/bin
amensaywho@CONSOLE:~/ROOT_Tutorial$ echo $LD_LIBRARY_PATH
/home/amensaywho/geant4/geant4-v11.2.2-install/lib:/home/amensaywho/root/lib

```

Related question: running another macro, given below, returns the error: `Error in <TTree::Branch>: The class requested (vector<MyEvent>) for the branch "Events" is an instance of an stl collection and does not have a compiled CollectionProxy. Please generate the dictionary for this collection (vector<MyEvent>) to avoid to write corrupted data.`

tree\_example10.C:

```auto
//Tree with vector<MyEvent> as branch
#include <vector>
#include "MyEvent.h"

void tree_example10() {

  TFile *f = new TFile("myfile_example10.root", "RECREATE");
  TTree *T = new TTree("T","tree with vector");
  TRandom r;
  std::vector<MyEvent> *events = new std::vector<MyEvent>();

  T->Branch("Events", "std::vector<MyEvent>", &events);

  for (Int_t i = 0; i < 1000; i++) {

    //clean vectors for each event
    events->clear();
    //Fill vectors for each event
    Float_t px, py;
    Int_t np = (Int_t)(r.Rndm()*10);
    for(Int_t j = 0; j < np; j++){
      r.Rannor(px, py);
      Float_t pt = std::sqrt(px*px + py*py);
      MyEvent event;
      event.setPx(px);
      event.setPy(py);
      event.setPt(pt);
      events->push_back(event);
    }
    T->Fill();
  }
  
  f->Write();
  f->Close();

  delete events;
}

```

The header file for MyEvent.h is same as above. How do I resolve this? Would be great if someone could explain the error as well

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [October 18, 2024, 10:02am UTC](https://root-forum.cern.ch/t/ambiguous-expansion-of-macro-classdef/61958/7 "2024-10-18T10:02:53Z")

</div>

> [@amansas](#):
>
> Info for my ROOT Installation:

It all look consistent, so I am not sure why you are seeing this problem :(.

```auto
Related question: running another macro, given below, returns the error: Error in <TTree::Branch>: The class requested (vector<MyEvent>) for the branch "Events" is an instance of an stl collection and does not have a compiled CollectionProxy. Please generate the dictionary for this collection (vector<MyEvent>) to avoid to write corrupted data.

```

When storing a ‘standalone’ vector you need to explicitly request the dictionary for it.  
Add to your `MyEvent.h`:

```auto
#ifdef __ROOTCLING__
#pragma link C++ class std::vector<MyEvent>+;
#endif

```

---

<div class="post-metadata">

**Author:** ![amansas](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/3ec8ea/32.png) [@amansas](https://root-forum.cern.ch/u/amansas)\
**Post date:** [October 18, 2024, 10:07am UTC](https://root-forum.cern.ch/t/ambiguous-expansion-of-macro-classdef/61958/8 "2024-10-18T10:07:10Z")

</div>

> [@pcanal](#):
>
> It all look consistent, so I am not sure why you are seeing this problem :(.

I understand, no problem and thank you 🙂

> [@pcanal](#):
>
> When storing a ‘standalone’ vector you need to explicitly request the dictionary for it.  
> Add to your `MyEvent.h`:

Huh, so I have to add said lines for every Event Class I define (if i want to use said class in a vector)? Are there ways where I don’t have to explicitly request the dictionary for the vector?

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [October 18, 2024, 12:06pm UTC](https://root-forum.cern.ch/t/ambiguous-expansion-of-macro-classdef/61958/9 "2024-10-18T12:06:28Z")

</div>

Yes. When you use the syntax `.L ...+` one of the implicit operation is to add the equivalent of the `pragma link` line for the classes declared in the the file. In addition when generating the dictionary for a class, the system will also automatically generate the dictionary for any collection it uses. So if you add to your `MyEvent.h` the class:

```auto
class EventHolder {
    std::vector<Event> f1;
    std::list<Event> f2;
};

```

the dictionaries for `std::vector<Event>` and `std::list<Event>` will be automatically generated.
