0

I'm trying to build a SNES emulator in C++, using this tutorial as inspiration re: how to set up my data structures. In the video, he creates an array of structs (representing CPU instructions) and initializes it using an initializer list. However, when I try to do the same using a map<>, I get an error reporting that the compiler "could not convert" my initializer list to a map. What am I doing wrong? Below is my code; for context, I have typedef'd unsigned char to byte.

struct instruction {
    byte (SNES_CPU::*op)(void) = nullptr;
    byte (SNES_CPU::*mode)(void) = nullptr;
    byte cycles = 0;
};
    
using cpu = SNES_CPU;
std::map<byte, struct instruction> ops = {
    {0x69, {&cpu::ADC, &cpu::IMM8, 2}},
    {0x6D, {&cpu::ADC, &cpu::ABS8, 4}}
};

Additionally, when I try to set individual entries in the map to a struct instruction with an initializer list, it gives me a similar error.

ops[0x69] = {&cpu::ADC, &cpu::IMM8, 2};

I'm working on Raspbian, and compiling with G++ 8.3.0.

Max Farr
  • 9
  • 2
  • See [this](https://stackoverflow.com/questions/3250123/what-would-a-stdmap-extended-initializer-list-look-like) possible duplicate. You don't need the inner curly brackets. – 1201ProgramAlarm Dec 07 '20 at 21:12
  • You can use: `std::make_pair(0x69, {&cpu::ADC, &cpu::IMM8, 2});` for each element. – bloody Dec 07 '20 at 21:16
  • Cannot reproduce: https://godbolt.org/z/Pd9TEb – user4581301 Dec 07 '20 at 21:19
  • @1201ProgramAlarm, which inner curly brackets? I can't seem to get this to work. – Max Farr Dec 08 '20 at 06:13
  • @bloody, I tried that but it still gives me a whole mess of errors. Most notably, it says that it can't convert a to type 'SNES_CPU::instruction&&'. – Max Farr Dec 08 '20 at 06:14
  • Never mind, everyone! I managed to solve it: my "op" function was returning a bool rather than a byte and the compiler couldn't resolve the discrepancy. I'm new to posting here - is it good practice to update my original question to mark it as solved, or can I leave this thread as it is? – Max Farr Dec 08 '20 at 06:26
  • For this to be a good question, it would [edit]ed to include the full compiler error message, the declarations of the class you're using (`cpu`) with the minimum details required for the question (the member functions you're referencing). That would then be a [mre]. Even with all that, the solution is potentially a typo, although with the full error message in the question another user could find this question and realize what their problem is. With those edits, a [self answer](https://stackoverflow.com/help/self-answer) is allowable. – 1201ProgramAlarm Dec 08 '20 at 19:43

0 Answers0