1
votes
vector< vector< vector<int> > > myArray(5, vector< vector<int> >(4));
vector<int> testArray();
myArray[0][0].push_back(testArray);

I don't understand. I'm just trying to append a new element to it.

Edit: Second line was wrong but this still doesn't compile.

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector< vector< vector<int> > > myArray(5, vector< vector<int> >(4));
    vector<int> testArray;
    myArray[0][0].push_back(testArray);
    return 0;
}

The compile error:

pnt.cpp: In function ‘int main()’: pnt.cpp:8: error: no matching function for call to ‘std::vector >::push_back(std::vector >&)’ /usr/include/c++/4.4/bits/stl_vector.h:733: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = int, _Alloc = std::allocator]

2
I don't know of a better way to do it. I'm porting some Python code because it was inefficient. This is the fastest way I could think of that still seemed somewhat clean.cBMtb
what error are you seeing? Is it a compile error or a runtime problem?Tim

2 Answers

9
votes
vector<int> testArray();

Should be:

vector<int> testArray;

vector<int> testArray(); is a forward declaration of a function called testArray which returns vector<int>.

You also have one level of indirection too much:

myArray[0].push_back(testArray);

or

myArray[0][0] = testArray;
1
votes

myArray is a vector of vector of vector of int. myArray[0] is a vector of vector of int. This is where you need to push_back your vector of int, like so:

std::vector< std::vector< std::vector<int> > > myArray(5, std::vector< std::vector<int> >(4));
std::vector<int> testArray;
myArray[0].push_back(testArray);
return 0;

With myArray[0][0] you are accessing the vector of int, not vector of vector of int.