The error message is telling you that you are trying to assign an int to an int*. Specifically, on this statement:
storage[i][j][k]=k;
storage[i][j][k] returns (a reference to) an int*, but k is an int.
Since you have 3 levels of vectors containing an int[] array, you need 4 loops to initialize the individual ints, but you only have 3 loops, so add another loop:
#include <iostream> #include <vector> using namespace std; int main() { vector<vector<vector<int*>>> storage; for (int i = 0; i < 13; ++i) { storage.push_back(vector<vector<int*>>()); for (int j = 0; j < 13; ++j) { storage[i].push_back(vector<int*>()); for(int k = 0; k < N; ++k) // <-- decide what N should be! { storage[i][j].push_back(new int[5]); for (int m = 0; m < 5; ++m) { storage[i][j][k][m] = k; } } } } // don't forget to delete[] all of the new[]'ed arrays! // consider using either std::unique_ptr<int[]> or // std::array<int,5> instead of int* ... return 0; }
I would suggest simplifying the code to make it more readable, eg:
#include <iostream> #include <vector> #include <array> using namespace std; using arr5Ints = array<int, 5>; using vec1D_arr5Ints = vector<arr5Ints>; using vec2D_arr5Ints = vector<vec1D_arr5Ints>; using vec3D_arr5Ints = vector<vec2D_arr5Ints>; int main() { vec3D_arr5Ints storage( 13, vec2D_arr5Ints( 13, vec1D_arr5Ints(N) // <-- decide what N should be! ) ); for (auto &vec2d : storage) { for (auto &vec1d : vec2d) { for(auto &arr : vec1d) { int k = 0; for (int &i : arr) { i = k++; } } } } return 0; }
intandint*.(int*)malloc(...)) you should take that as a sign that you're doing something wrong.[]. If you want 3D, you have one layer of arrays too many. (In this case I recommend removing*andmalloc. By the waymalloc.his a wrong header, you needstdlib.h) The fact that you havestorage[i]andstorage[i][j]at the same indentation level right next to each other is a hint that something fishy is going on.std::vectorfor most of your object's dimensions, but are required to use the very C++ unfriendlymallocfor exactly one of the dimensions.