I was creating empty 2D vector in a header file by just providing size but unable to create it.
class Grid
{
public:
int rows = 5/0.05;
int cols = 6/0.05;
std::vector<std::vector<uint8_t>> grid(rows, std::vector<uint8_t>(cols, 0));
};
I am getting below error
no matching function for call to 'std::vector<std::vector<unsigned char> >::vector(int&, std::vector<int>)'
`error: 'rows' is not a type
19 | std::vector<std::vector<uint8_t>> grid(rows, std::vector<uint8_t>(cols, 0));
| ^~~~
C:\Users\prave\Documents\projects\ExhaustiveSearchScanMatching\occupancy_grid.h:19:79: error: expected ')' before ',' token
19 | std::vector<std::vector<uint8_t>> grid(rows, std::vector<uint8_t>(cols, 0));
| ~ ^
| )
C:\Users\prave\Documents\projects\ExhaustiveSearchScanMatching\occupancy_grid.h:19:79: error: expected ')' before ',' token
19 | std::vector<std::vector<uint8_t>> grid(rows, std::vector<uint8_t>(cols, 0));
| ~ ^
| )
C:\Users\prave\Documents\projects\ExhaustiveSearchScanMatching\occupancy_grid.h:19:81: error: expected unqualified-id before numeric constant
19 | std::vector<std::vector<uint8_t>> grid(rows, std::vector<uint8_t>(cols, 0));
| ^`
Can anyone please help to fix this issue.
You have used two different integer types uint8_t
and int
.
You should change it as
std::vector<std::vector<uint8_t>> grid(rows, std::vector<uint8_t>(cols, 0));
As A Class Member To initialize the vector as a class member that is also dependent on other class members you can use the class constructor. The following code worked for me.
#include <vector>
#include <cstdint>
class grid {
public:
int rows = 5/0.05;
int cols = 6/0.05;
std::vector<std::vector<uint8_t>> grid_vector;
grid() : grid_vector(rows, std::vector<uint8_t>(cols, 0)) {}
};