我有一段用 C++ 编写并使用 make 编译的简单代码。
我编译时没有出错。但是当我运行它时,出现由 std::vector< std::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框架的方法
apv_q 定义为 std::vector< std::vector<short> > *apv_q;
我对生成字典不熟悉,于是上网搜索,发现建议在头文件中加入下面几行
#ifdef __MAKECINT__
#pragma link C++ class vector<short> +;
#pragma link C++ class vector<vector<short> >+;
#endif
但它不起作用!
所以我需要你的帮助来解决这个问题,请帮忙!
提前致谢!
干杯,
埃达
如果相关,下面是我的 MakeFile:
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 $@ $(CXXFLAGS) $(ADDCXXFLAGS) $(HITMAKEROBJS) $(LDFLAGS) $(LIBS)
%.o: %.cc $(HDRS)
$(CXX) $(CXXFLAGS) $(ADDCXXFLAGS) -c $<
最佳答案
你必须运行 root-cint
并给它一个头文件列表,包括 LinkDef.h
其中包含行
#pragma link C++ class vector<short> +;
#pragma link C++ class vector<vector<short> >+;
此工具随后会创建一个源文件,您可以编译该文件并将其链接到您的项目。
您可能希望将此任务包含在您的 Makefile 中,如果您想使用它,可以使用一些可用于 CMake 的宏(搜索 FindRoot.cmake
,这包括宏 ROOT_GENERATE_DICTIONARY
)。
编辑:它对我有用,当我运行时
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();
然后读回
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
被写入标准输出。
关于c++ - 如何为类生成字典(vector<vector<short>>),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37566747/