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 &} '"