Vector of vectors of TH1F* initialization in constructor

Hi,

I have a class with private member std::vector<std::vector<TH1F*> > hMissPtCent;
I am trying to initialize this in the constructor simply like hMissPtCent = 0;. I then call a function that does the following

int MyClass::InitMissMomHistos() {
for(int imom; imom<missMomBins; imom++){
for(int icent=0; icent<ncent; icent++){
sprintf(chMissPtCent,"hMissPt_%i_Cent_%i",imom,icent);
hMissPtCent[imom][icent] = new TH1F(chMissPtCent,chMissPtCent,1000,-100.0,100.0);
}
return 1;
}

Using a makefile, I try to compile but get the following errors

TrigGlobalBranch.C:724: error: no match for 'operator=' in '((TrigGlobalBranch*)this)->TrigGlobalBranch::hMissPtCent = 0'
/afs/cern.ch/sw/lcg/contrib/gcc/4.3.2/x86_64-slc5-gcc34-opt/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.3.2/../../../../include/c++/4.3.2/bits/vector.tcc:144: note: candidates are: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = std::vector<TH1F*, std::allocator<TH1F*> >, _Alloc = std::allocator<std::vector<TH1F*, std::allocator<TH1F*> > >]

any ideas on what the problem is?

I suppose replacing the vector of vectors with a 2D array would work, but this still doesn’t explain the errors I’m having. (Also, the first loop in the above code contains int imom=0; not just int imom;)

Compiler gives you quite good and clear diagnostic: vector does not have operator = which can assign integer value 0 to vector. std::vector actually has copy assignment operator (and with C++11 move assignment operator, in some implementations operator = from std::initializer_list), but none of them can be used (en.cppreference.com/w/cpp/contai … perator%3D)

I suggest you read about vector if you want to use it, also,it would be good to read about constructors, operators (to understand why your assignment expression contains error), memory management, etc.

For example, vector has its own constructors (en.cppreference.com/w/cpp/contai … tor/vector , also note ‘explicit’ here, it’s important!) and you do not have to initialize it like you are trying to do - the work is done by default constructor (if you do not pass any parameters in a member- initializer -list) - the vector will be empty and correctly initialized.

And later, before you can assign something to vector elements in your InitMissMomHistos using operator [], you have to resize vectors (and nested vectors) to the correct size. Otherwise, you will overwrite memory you do not own with some unpredictable results.

Example, how to use vector of vectors (still quite ugly but at least shows the idea)

[code]class A
{
public:
A()
{
//vector is correctly initialized, it has default ctor.

 ///////

}
void init(int nRows, int nCols …)
{
typedef std::vector::size_type size_type;
data_.resize(nRows);
for(size_type i = 0; i < nRows; ++i) {
data_[i].resize(nCols);
//init elements in a nested loop.
}
}
private:
std::vector<std::vector<TH1F *> > data_;
};[/code]

P.S.
Also, vector has ‘clear’ member function to reset its size to 0 (it’s not required by standard that vector’s memory block is released, so ‘capacity’ can be still not 0), you can assign an empty vector to your vector, you can use swap trick with empty vector, you can use (with C++11) shrink_to_fit method to modify real capacity etc.
If you really want to use assign operator for this, do:
hiMissPtCent = std::vector<std::vector<TH1F *> >(0);

P.P.S.

Or … switch to C++11 and do it like this
hiMissPtCent = {0};

:slight_smile:)))