Hi
I´m writing the configuration system for my game. I need a function that returns the name of a given key. This is my code:
#include <array>
#include <string>
#include <SFML/Window/Keyboard.hpp>
...
std::string ige::AdmEntrada::obtNombreTecla (const sf::Keyboard::Key tecla) {
// Array con los nombres de todas las teclas en castellano
const static std::array <std::string, sf::Keyboard::Key::KeyCount - 1> nombreTeclas = {
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
"R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"Cero", "Uno", "Dos", "Tres", "Cuatro", "Cinco", "Seis", "Siete", "Ocho", "Nueve",
"Esc",
"Ctrl Izq.", "May. Izq.", "Alt Izq.", "Sistema Izq.",
"Ctrl Der.", "May. Der.", "Alt Der.", "Sistema Der.",
"Menu", "Abre cor.", "Cierra cor.", "Punto y coma", "Coma", "Punto",
"Comillas", "Barra", "Barra inv.", "Tilde", "Igual", "Guion",
"Barra espaciadora", "Enter", "Backspace", "Tab", "Page Up", "Page Down",
"Fin", "Inicio", "Insertar", "Suprimir",
"Agregar", "Restar", "Multiplicar", "Dividir",
"Flecha Izq.", "Flecha Der.", "Flecha Arriba", "Flecha Abajo",
"0 Numpad", "1 Numpad", "2 Numpad", "3 Numpad", "4 Numpad",
"5 Numpad", "6 Numpad", "7 Numpad", "8 Numpad", "9 Numpad",
"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10",
"F11", "F12", "F13", "F14", "F15", "Pausa"
};
return nombreTeclas [tecla];
}
I got 1 error and 1 warning:
E:\Programacion\TV Circo\JUEGO\AdmEntrada.cpp|389|warning: missing braces around initializer for 'std::array<std::basic_string<char>, 100u>::value_type [100] {aka std::basic_string<char> [100]}' [-Wmissing-braces]|
E:\Programacion\TV Circo\JUEGO\AdmEntrada.cpp|389|error: too many initializers for 'const std::array<std::basic_string<char>, 100u>'|
||=== Build finished: 1 errors, 1 warnings (0 minutes, 0 seconds) ===|
How can I initialize the std::array correctly? It doesnt support std::initializer_list ctor as far as I know. Adding braces thows only the error (too many initializers)
I´m using MinGW/GCC 4.8.0 (c++0x enabled), SFML 2.0
Try double-braces:
const static std::array <std::string, sf::Keyboard::Key::KeyCount - 1> nombreTeclas = {{ /* stuff here */ }};
The reason for this is because std::array<T, n> is equivalent to this:
struct array {
T values[n];
};
That said, in this context I'm pretty sure it's supposed to work without the extra braces, so this may be a compiler bug. If nothing else works, you could always just use a bare array.