push_back in Vector 2D, emitting error "no matching Function for call"

Asked

Viewed 197 times

0

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

As this question answered here, it is possible to use std::vector, 2D (grid) with push_back() directly (variavel[indice].push_back(...)) increasing one-dimensionally, 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 above code is real, but perforated (abstract), since it is inserted within a context of more than 3 thousand lines.

There is an error in compile time, issued exactly to the line containing the 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 very limited. In case there is any idea of solution I am already grateful.

Two things need to be highlighted from this code first "no matching Function for call" and then, just below, a note that informs "no known Conversion for argument 1 from ːTcoord {aka Scoord}' to ːconst value_type& {aka const Std::vector<Scoord>&}'"

1 answer

0

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

While searching 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 improperly, in this case *Grid[0][0].

It is therefore concluded that Grid[i] accesses directly std::vector<std::vector<TCoord>>, independently of "i", therefore, push_back() must receive a std::vector<TCoord>, as a *Grid[i], is the 2D vector itself, std::vector<std::vector<TCoord>>, in its index "i" which is a std::vector<TCoord> and that receives in push_back(), a Tcoord.

A way to access would also be Grid->at(i)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.