"has no member named ..." compile error
Dec 2, 2016 at 8:35pm UTC
I am new to vectors and this example get compile error on line 28:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <iostream>
#include <vector>
struct Map
{
int rowCol;
};
struct Row
{
std::vector<Map> layout_to_matrix_map;
};
struct Matrix
{
std::vector<Row> vRows;
void init()
{
for (int row=0; row<3; row++)
{
std::vector<int > vTempCells;
for (int cell=0; cell<2; cell++)
{
vTempCells.push_back(cell);
}
vRows.layout_to_matrix_map.push_back(vTempCells); //error: ‘class std::vector<Row>’ has no member named ‘layout_to_matrix_map’
}
}
};
int main()
{
Matrix matrix;
matrix.init();
std::cout << "\noutput using array style:\n" ;
for (std::vector<unsigned int >::size_type row = 0; row < matrix.vRows.size(); row++)
{
std::cout << "cells = " ;
for (std::vector<unsigned int >::size_type cell=0; cell < matrix.vRows[row].layout_to_matrix_map.size(); cell++)
{
std::cout << matrix.vRows[row].layout_to_matrix_map[cell].rowCol << " " ;
}
std::cout << std::endl;
}
}
Why does it get this compile error on line 28?:
1 2 3 4 5
$ g++ noMember.cpp
noMember.cpp: In member function ‘void Matrix::init()’:
noMember.cpp:32:19: error: ‘class std::vector<Row>’ has no member named ‘layout_to_matrix_map’
vRows.layout_to_matrix_map.push_back(vTempCells);
^~~~~~~~~~~~~~~~~~~~
Last edited on Dec 2, 2016 at 8:38pm UTC
Dec 2, 2016 at 9:17pm UTC
True, std::vector does not have member named 'layout_to_matrix_map'.
Similarly,
1 2
Row foo[42];
foo.layout_to_matrix_map.push_back(vTempCells); // syntax error
A Row has such member. Every element of foo is a Row. Compare line 28 to lines 44 and 46, where you do dereference elements of matrix.vRows.
However, the matrix.vRows is empty. It has no elements. No Row objects. You never add any.
Why does the Matrix has a member init()? You could do the same operations in the constructor of Matrix.
Dec 2, 2016 at 10:20pm UTC
Thanks keskiverto.
I added Row element to vRows and moved init() to Matrix constructor.
Now there is a different compile error on line 26:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <iostream>
#include <vector>
struct Map
{
int rowCol;
};
struct Row
{
std::vector<Map> layout_to_matrix_map;
};
struct Matrix
{
std::vector<Row> vRows;
Matrix()
{
for (int row=0; row<3; row++)
{
vRows.push_back(Row());
for (int cell=0; cell<2; cell++)
{
vRows[row].layout_to_matrix_map.push_back(cell); //compile error
}
}
}
};
int main()
{
Matrix matrix;
std::cout << "\noutput using array style:\n" ;
for (std::vector<unsigned int >::size_type row = 0; row < matrix.vRows.size(); row++)
{
std::cout << "cells = " ;
for (std::vector<unsigned int >::size_type cell=0; cell < matrix.vRows[row].layout_to_matrix_map.size(); cell++)
{
std::cout << matrix.vRows[row].layout_to_matrix_map[cell].rowCol << " " ;
}
std::cout << std::endl;
}
}
compile error:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
$ g++ noMember.cpp
noMember.cpp: In constructor ‘Matrix::Matrix()’:
noMember.cpp:30:63: error: no matching function for call to ‘std::vector<Map>::push_back(int &)’
vRows[row].layout_to_matrix_map.push_back(cell);
^
In file included from /usr/include/c++/6.2.1/vector:64:0,
from noMember.cpp:6:
/usr/include/c++/6.2.1/bits/stl_vector.h:914:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Map; _Alloc = std::allocator<Map>; std::vector<_Tp, _Alloc>::value_type = Map]
push_back(const value_type& __x)
^~~~~~~~~
/usr/include/c++/6.2.1/bits/stl_vector.h:914:7: note: no known conversion for argument 1 from ‘int ’ to ‘const value_type& {aka const Map&}’
/usr/include/c++/6.2.1/bits/stl_vector.h:932:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = Map; _Alloc = std::allocator<Map>; std::vector<_Tp, _Alloc>::value_type = Map]
push_back(value_type&& __x)
^~~~~~~~~
/usr/include/c++/6.2.1/bits/stl_vector.h:932:7: note: no known conversion for argument 1 from ‘int ’ to ‘std::vector<Map>::value_type&& {aka Map&&}’
Dec 2, 2016 at 10:36pm UTC
You are pushing 'cell' (which is an int
) onto 'layout_to_matrix_map' (which is a vector or Map objects). Integers aren't maps.
Dec 2, 2016 at 11:22pm UTC
Try to do something like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
struct Matrix
{
std::vector<Row> vRows;
Matrix()
{
Map tempmap;
for (int row = 0; row<3; row++)
{
vRows.push_back(Row());
for (int cell = 0; cell<2; cell++)
{
tempmap.rowCol = cell;
vRows[row].layout_to_matrix_map.push_back(tempmap);
}
}
}
};
Dec 2, 2016 at 11:25pm UTC
Thanks xismn.
But a new compile error on line 30 has me even more baffled.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <iostream>
#include <vector>
struct Map
{
int rowCol;
Map(int rowCol) : rowCol(rowCol) {}
};
struct Row
{
std::vector<Map> layout_to_matrix_map;
};
struct Matrix
{
std::vector<Row> vRows;
Matrix()
{
for (int row=0; row<3; row++)
{
std::vector<Map> vTempMap;
for (int cell=0; cell<2; cell++)
{
Map tempMap(cell);
vTempMap.push_back(tempMap);
}
vRows[row].layout_to_matrix_map.push_back(vTempMap); //compile error
}
}
};
int main()
{
Matrix matrix;
std::cout << "\noutput using array style:\n" ;
for (std::vector<unsigned int >::size_type row = 0; row < matrix.vRows.size(); row++)
{
std::cout << "cells = " ;
for (std::vector<unsigned int >::size_type cell=0; cell < matrix.vRows[row].layout_to_matrix_map.size(); cell++)
{
std::cout << matrix.vRows[row].layout_to_matrix_map[cell].rowCol << " " ;
}
std::cout << std::endl;
}
}
compile error on line 30:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
$ g++ noMember.cpp
noMember.cpp: In constructor ‘Matrix::Matrix()’:
noMember.cpp:35:63: error: no matching function for call to ‘std::vector<Map>::push_back(std::vector<Map>&)’
vRows[row].layout_to_matrix_map.push_back(vTempMap);
^
In file included from /usr/include/c++/6.2.1/vector:64:0,
from noMember.cpp:6:
/usr/include/c++/6.2.1/bits/stl_vector.h:914:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Map; _Alloc = std::allocator<Map>; std::vector<_Tp, _Alloc>::value_type = Map]
push_back(const value_type& __x)
^~~~~~~~~
/usr/include/c++/6.2.1/bits/stl_vector.h:914:7: note: no known conversion for argument 1 from ‘std::vector<Map>’ to ‘const value_type& {aka const Map&}’
/usr/include/c++/6.2.1/bits/stl_vector.h:932:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = Map; _Alloc = std::allocator<Map>; std::vector<_Tp, _Alloc>::value_type = Map]
push_back(value_type&& __x)
^~~~~~~~~
/usr/include/c++/6.2.1/bits/stl_vector.h:932:7: note: no known conversion for argument 1 from ‘std::vector<Map>’ to ‘std::vector<Map>::value_type&& {aka Map&&}’
Last edited on Dec 2, 2016 at 11:26pm UTC
Dec 2, 2016 at 11:31pm UTC
The vTempMap is a vector (of Maps). You try to push it to vector layout_to_matrix_map, which stores plain Map objects, not vectors.
Dec 3, 2016 at 12:12am UTC
You are right keskiverto.
And that is exactly what Akimov's solution solved.
Thank you all for your helps. :-)
This runs as intended:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <iostream>
#include <vector>
struct Map
{
int rowCol;
};
struct Row
{
std::vector<Map> layout_to_matrix_map;
};
struct Matrix
{
std::vector<Row> vRows;
Matrix()
{
Map tempMap;
for (int row=0; row<3; row++)
{
vRows.push_back(Row());
for (int cell=0; cell<2; cell++)
{
tempMap.rowCol = cell;
vRows[row].layout_to_matrix_map.push_back(tempMap);
}
}
}
};
int main()
{
Matrix matrix;
std::cout << "\noutput using array style:\n" ;
for (std::vector<unsigned int >::size_type row = 0; row < matrix.vRows.size(); row++)
{
std::cout << "row=" << row << ", cells = " ;
for (std::vector<unsigned int >::size_type cell=0; cell < matrix.vRows[row].layout_to_matrix_map.size(); cell++)
{
std::cout << matrix.vRows[row].layout_to_matrix_map[cell].rowCol << " " ;
}
std::cout << std::endl;
}
}
output:
1 2 3 4
output using array style:
row=0, cells = 0 1
row=1, cells = 0 1
row=2, cells = 0 1
Topic archived. No new replies allowed.