2

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.

alfC
  • 14,261
  • 4
  • 67
  • 118

3 Answers3

2

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)) {}
};
tugrul
  • 135
  • 3
2

You're trying to create a vector of vectors of uint8_t. Then you are constructing that vector with a vector of type int where it is expecting a vector of type uint_8, hence the no matching function error message.

Either change your vector to be a vector of vectors of int:

std::vector<std::vector<int>> grid(rows, std::vector<int> (cols, 0));

Or change your construction argument to be a vector of type uint8_t:

std::vector<std::vector<uint8_t>> grid(rows, std::vector<uint8_t>(cols, 0));
WalleyM
  • 172
  • 7
2

This initialization

    std::vector<std::vector<uint8_t>> grid(rows, std::vector<uint8_t>(cols, 0));

of a class data member is incorrect. The compiler considers such a declaration as a declaration of a member function.

You may use brace or equal initialization. For example

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)};

};

That is enclose the initializer in braces instead of parentheses.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335