2016-06-01 59 views
0

我有一個簡單的代碼,我用C++編寫並使用make進行編譯。 我在編譯時沒有遇到錯誤。但是,當我運行它,我得到引起std::vector< std::vector<short> >一個錯誤,請參閱下面的錯誤:如何爲類生成字典(vector <vector <short>>)

Error in <TTree::SetBranchAddress> : Unable to determine the type given for the address for "apv_q". The class expected (vector<vector<short> >) refers to an stl collection and do not have a compiled CollectionProxy. Please generate the dictionary for this class (vector<vector<short> >)

TTree::SetBranchAddress來自CERN的ROOT框架的方法爲std::vector< std::vector<short> > *apv_q;

apv_q定義不熟悉生成字典,所以我在網上搜索,發現在頭文件中添加下面幾行的建議:

#ifdef __MAKECINT__ 
#pragma link C++ class vector<short> +; 
#pragma link C++ class vector<vector<short> >+; 
#endif 

但它不起作用!

所以我需要你的幫助來解決這個問題,請大家幫忙!

非常感謝!

乾杯,

江田

如果相關,下面是我的生成文件:

 CONFIG=root-config 
     CXXFLAGS=$(shell $(CONFIG) --cflags) 
     LIBS=$(shell $(CONFIG) --glibs) 
     LDFLAGS=$(shell $(CONFIG) --ldflags) 
     CXX=g++ 
     ADDCXXFLAGS=-ggdb -O0 -std=c++0x 

     HDRS= ./Settings.h ./HitMaker.h 

     HITMAKEROBJS= HitMaker.o 

     all: hitmaker 

     hitmaker: $(HITMAKEROBJS) 
       $(CXX) -o [email protected] $(CXXFLAGS) $(ADDCXXFLAGS) $(HITMAKEROBJS) $(LDFLAGS) $(LIBS) 

     %.o: %.cc $(HDRS) 
       $(CXX) $(CXXFLAGS) $(ADDCXXFLAGS) -c $< 

回答

0

你必須運行root-cint,並給它你的頭文件的列表,包括其中包含LinkDef.h

#pragma link C++ class vector<short> +; 
#pragma link C++ class vector<vector<short> >+; 

This工具然後創建一個源文件,您可以編譯並鏈接到您的項目。

您可能希望將此任務包含在您的Makefile中,如果您想使用它(搜索FindRoot.cmake,其中包括宏ROOT_GENERATE_DICTIONARY),則有一些可用於CMake的宏。

編輯:這對我的作品,當我運行

rootcint -f bla.cc -c HitMaker.h LinkDef.h 

,並添加在Makefile

HITMAKEROBJS=HitMaker.o bla.o 

我創建了

TFile file("test.root", "RECREATE"); 
TTree tree("tree", "treetitle"); 
std::vector<std::vector<short>> test; 
std::vector<short> test2; 
test2.push_back(1); 
test.push_back(test2); 
tree.Branch("test", &test); 
tree.Fill(); 
tree.Write(); 

root文件和讀回與

TFile file("test.root"); 
TTree* t= NULL; 
file.GetObject("tree", t); 
std::vector<std::vector<short>>* test = NULL; 
t->SetBranchAddress("test", &test); 
t->GetEvent(0); 
std::cout << test->front().front() << std::endl; 

將值1寫入stdout。

+0

順便說一句,ROOT有它自己的[容器類](https://root.cern.ch/doc/master/classTArrayS.html),所以它可能是替代使用那些'std :: vector' –

+0

非常感謝,但我有更多的問題。那麼,我檢查rootcint並做了'rootcint -f bla.cc -c HitMaker.h LinkDef.h'。它按預期創建了bla.cc和bla.h。我對編譯感到困惑,我改變了我的make文件以使得HDRS = $(HDIR)/Settings.h $(HDIR)/HitMaker.h $(HDIR)/ bla。h' 'HITMAKEROBJS = HitMaker.o' 但它給出了相同的錯誤。 如果我將bla.o添加到HITMAKEROBJS,那麼它會抱怨變量的多重定義。 我該怎麼辦? –

+0

您使用的是哪個版本的root?我在Debian 8上有5.34。 –

相關問題