'Problems a multidimensional array as part of a std::pair
I have a problem with forming a std::pair which I do not understand, maybe someone can explain it to me.
I have a typedef uint16_t Cursor_matrix[1][2];
In the following part of the program, I assign my Cursor_matrix variables to values, which I then want to connect to a std::string using a std::pair.
#include <iostream>
#include <utility>
#include <string>
#include <cstdint>
typedef uint16_t Cursor_matrix[1][2];
int main()
{
Cursor_matrix pos;
pos[0][0] = 1;
pos[0][1] = 2;
std::string str = "hello";
std::pair<Cursor_matrix, std::string> pos_text;
pos_text = std::make_pair(pos, str);
return 0;
}
*1: Here I get the following error message from the compiler (gcc 4,7,3)
source>:22:33: error: no match for 'operator=' (operand types are 'std::pair<short unsigned int [1][2], std::__cxx11::basic_string<char> >' and 'std::pair<short unsigned int (*)[2], std::__cxx11::basic_string<char> >')
The error message tells me, that the size of my Cursos_matrix is not known. However, this already exists as "pos".
SOLUTION:
Dont use native arrays with managed objects
using Cursor_matrix = std::array<std::array<uint16_t,2>, 1>;
int main()
{
Cursor_matrix pos;
pos[0][0] = 1;
pos[0][1] = 2;
std::string str = "hello";
std::pair<Cursor_matrix, std::string> pos_text;
pos_text = std::make_pair(pos, str);
return 0;
}
Thanks to WhozCraig!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
