push_back in Vector 2D, issuing error "no matching function for call"

0

I'm a beginner in C ++ programming, so I apologize in advance.

As this question is answered here , you can use std::vector , 2D ( grid) with push_back() directly ( variavel[indice].push_back(...) ) incrementing unidimensionally, and irregularly / disproportionately the columns and / or rows.

A struct:

typedef struct SCoord {
  unsigned int x, y; 

  SCoord(): x(0), y(0)
  {}
} TCoord;

In a certain part of the code, a 2D vector is initialized, as follows:

// CRIADOR DE POSIÇÕES
TCoord makePos(ulint x, ulint y){
  TCoord r;
  r.x = x;
  r.y = y;
  return r;
}

// Declaração da variável Grid, com tamanho de linha "gridlin" (inicializado)
std::vector<std::vector<TCoord>> *Grid = new std::vector<std::vector<TCoord>>(gridlin, std::vector<TCoord>());

/* ALGUMA PROCESSAMENTOS AQUI ...
 */

// Adição de celula/coluna à uma linha qualquer (certamente existente)
Grid[i].push_back(makePos(
  sortear(0, 50),
  sortear(0, 50)
));

The code above is real but punctuated (summary), since it is inserted within a context of more than 3 thousand lines.

There is a compile-time error, issued exactly to the line containing push_back() .

error: no matching function for call to ‘std::vector<std::vector<SCoord> >::push_back(TCoord)’ )); ^ note: candidates are: In file included from /usr/include/c++/4.8.2/vector:64:0, from /usr/include/c++/4.8.2/bits/random.h:34, from /usr/include/c++/4.8.2/random:50, from lib/rng.h:13, from lib/comum.h:17, from main.cpp:2: /usr/include/c++/4.8.2/bits/stl_vector.h:901:7: note: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::vector<SCoord>; _Alloc = std::allocator<std::vector<SCoord> >; std::vector<_Tp, _Alloc>::value_type = std::vector<SCoord>] push_back(const value_type& __x) ^ /usr/include/c++/4.8.2/bits/stl_vector.h:901:7: note: no known conversion for argument 1 from ‘TCoord {aka SCoord}’ to ‘const value_type& {aka const std::vector<SCoord>&}’ /usr/include/c++/4.8.2/bits/stl_vector.h:919:7: note: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::vector<SCoord>; _Alloc = std::allocator<std::vector<SCoord> >; std::vector<_Tp, _Alloc>::value_type = std::vector<SCoord>] push_back(value_type&& __x) ^ /usr/include/c++/4.8.2/bits/stl_vector.h:919:7: note: no known conversion for argument 1 from ‘TCoord {aka SCoord}’ to ‘std::vector<std::vector<SCoord> >::value_type&& {aka std::vector<SCoord>&&}’

I'm more used to dealing with the c pattern, using array and pointers, and my experience in c, especially C ++ (std :: vector) is quite limited. If there is any idea of a solution, I'm grateful.

Two things need to be highlighted in this code, first "matching function for call" and then, below, a note that informs "no known conversion for argument 1 from 'TCoord { aka SCoord} 'to' const value_type & {aka const std :: vector &} '"

    
asked by anonymous 01.08.2017 / 04:35

1 answer

0

Well, as mentioned, my knowledge in C ++ is limited.

When you search here and here , although I did not get all the knowledge involved in the subject, I realized that unlike array , as TCoord **Grid , which are accessed directly (referenced) with the operator [] >, as follows: Grid[0][0] ; a pointer to std::vector , needs to be accessed discon- tinuously, in this case *Grid[0][0] .

Thus, we conclude that Grid[i] directly access std::vector<std::vector<TCoord>> , regardless of "i", so push_back() must receive std::vector<TCoord> , while *Grid[i] , is the 2D vector itself, std::vector<std::vector<TCoord>> , in its index "i" which is a std::vector<TCoord> and receives a TCoord in push_back ().

An access form would also be Grid->at(i)

    
01.08.2017 / 22:46