c++ - 如何为类生成字典(vector<vector<short>>)

标签 c++ dictionary stl makefile root-framework

我有一段用 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/

相关文章:

c++ - 缩放 Qt3D 的相机

c++ - 运算符重载的未解析外部符号

python - 使用 Python 的最大值返回两个同样大的值

c++ - 用于类数据库搜索的容器

c++ - 在 C++11 基于范围的 'for' 循环中获取对 STL 容器元素的引用

C++ 双端队列 : when iterators are invalidated

c++ - 如何重命名 boost 属性树中的节点/元素?

C++:这种编译时多态性技术如何称呼?有什么优点和缺点?

python - 两个列表到一个字典

python - 使用 DictReader 从 CSV 文件读取到列表中