简体   繁体   中英

Error with Struct in C++

I'm having a problem with a struct when trying to do this in C++ (visual studio):

memoria.cpp

  struct readMem {
    UINT32 startAdress;
    UINT32 endAdress;
    UINT8(*memoryHandler) (UINT32);
    void *pUserData;
};

struct writeMem {
    UINT32 startAdress;
    UINT32 endAdress;
    void (*memoryHandler) (UINT32, UINT8);
    void *pUserData;
};



 void memoria::writeRAMBankEnable(UINT32 a, UINT8 b) {
}

void memoria::writeROMBankSelect(UINT32 a, UINT8 b) {
}

void memoria::writeRAMROMModeSelect(UINT32 a, UINT8 b) {
}

void memoria::writeVRAM(UINT32 a, UINT8 b) {
}

void memoria::writeSRAM(UINT32 a, UINT8 b) {
}

void memoria::writeRAM(UINT32 a, UINT8 b) {
}

void memoria::writeERAM(UINT32 a, UINT8 b) {
}

void memoria::writeSprite(UINT32 a, UINT8 b) {
}

void memoria::writeIOM(UINT32 a, UINT8 b) {
}

void memoria::writeHRAM(UINT32 a, UINT8 b) {
}


struct writeMem implWriteMem[] = {
    { 0x0000, 0x1FFF, writeRAMBankEnable, NULL},
    { 0x4000, 0x5FFF, writeROMBankSelect, NULL},
    { 0x6000, 0x7FFF, writeRAMROMModeSelect, NULL},
    { 0x8000, 0x9FFF, writeVRAM, NULL},
    { 0xA000, 0xBFFF, writeSRAM, NULL},
    { 0xC000, 0xDFFF, writeRAM, NULL},
    { 0xE000, 0xFDFF, writeERAM, NULL},
    { 0xFE00, 0xFE9F, writeSprite, NULL},
    { 0xFF00, 0xFF7F, writeIOM, NULL},
    { 0xFF80, 0xFFFF, writeHRAM, NULL},
    { (UINT32) - 1, (UINT32) - 1, NULL, NULL}
};

    memoria.h    

        #pragma once

#include <stdlib.h>
#include <stdio.h>
#include "defs.h"

ref class memoria
{
public:
    memoria(void);

private:
    FILE *file;
    UINT8 *mem;
public:

    void writeRAMBankEnable(UINT32, UINT8);

    void writeROMBankSelect(UINT32, UINT8);

    void writeRAMROMModeSelect(UINT32, UINT8);

    void writeVRAM(UINT32, UINT8);

    void writeSRAM(UINT32, UINT8);

    void writeRAM(UINT32, UINT8);

    void writeERAM(UINT32, UINT8);

    void writeSprite(UINT32, UINT8);

    void writeIOM(UINT32, UINT8);

    void writeHRAM(UINT32, UINT8);

    UINT8 readRAMBankEnable(UINT32);

    UINT8 readROMBankSelect(UINT32);

    UINT8 readRAMROMModeSelect(UINT32);

    UINT8 readVRAM(UINT32);

    UINT8 readSRAM(UINT32);

    UINT8 readRAM(UINT32);

    UINT8 readERAM(UINT32);

    UINT8 readSprite(UINT32);

    UINT8 readIOM(UINT32);

    UINT8 readHRAM(UINT32);


    void Meminitialize();
    void MemcleanUp();

    void writeByte(UINT32, UINT8);
    UINT8 readByte(UINT32);

    void writeWord(UINT32, UINT16);
    UINT16 readWord(UINT32);
};

Visual studio c++ gives me this errors:

1>memoria.cpp(75): error C2065: 'writeRAMBankEnable' : undeclared identifier

1>memoria.cpp(76): error C2065: 'writeROMBankSelect' : undeclared identifier

1>memoria.cpp(77): error C2065: 'writeRAMROMModeSelect' : undeclared identifier

1>memoria.cpp(78): error C2065: 'writeVRAM' : undeclared identifier

1>memoria.cpp(79): error C2065: 'writeSRAM' : undeclared identifier

1>memoria.cpp(80): error C2065: 'writeRAM' : undeclared identifier

1>memoria.cpp(81): error C2065: 'writeERAM' : undeclared identifier

1>memoria.cpp(82): error C2065: 'writeSprite' : undeclared identifier

1>memoria.cpp(83): error C2065: 'writeIOM' : undeclared identifier

1>memoria.cpp(84): error C2065: 'writeHRAM' : undeclared identifier

1>memoria.cpp(169): error C2065: 'implReadMem' : undeclared identifier

1>memoria.cpp(179): error C2065: 'implReadMem' : undeclared identifier

For the record, I have declared all the functions in my memoria.h, all but the structs and the implWriteMem[], of course. Anyway, how can I solve that?

PS It worked quite well in pure C.

Thanks !

{ 0x0000, 0x1FFF, writeRAMBankEnable, NULL},

Did you perhaps mean

{ 0x0000, 0x1FFF, &memoria::writeRAMBankEnable, NULL},

You have to include the class name when referring to a member, except within the class. And when making a pointer to a member function, you always need the class name.

The best solution is to use the polymorphism features built into the language:

ref struct MemoryRegion abstract
{
     virtual uint8_t Read( uint32_t address ) = 0;
     virtual void Write( uint32_t address, uint8_t value ) = 0;
     const uint32_t start_address, end_address;
protected:
     MemoryRegion( uint32_t start, uint32_t end ) : start_address(start), end_address(end) {}
};

ref struct SRAM : MemoryRegion
{
     SRAM( uint32_t start, uint32_t end ) : MemoryRegion(start, end) {}
     virtual uint8_t Read(uint32_t address);
     virtual void Write(uint32_t address, uint8_t value);
};

ref struct RAM : MemoryRegion
{
     RAM( uint32_t start, uint32_t end ) : MemoryRegion(start, end) {}
     virtual uint8_t Read(uint32_t address);
     virtual void Write(uint32_t address, uint8_t value);
};

ref struct VRAM : MemoryRegion
{
     VRAM( uint32_t start, uint32_t end ) : MemoryRegion(start, end) {}
     virtual uint8_t Read(uint32_t address);
     virtual void Write(uint32_t address, uint8_t value);
};

and so on. Then you can make an array of handles to the base type, and fill in all kinds of behavior-specific classes:

array<MemoryRegion^>^ memories = gcnew array<MemoryRegion^>(10);
memories[0] = gcnew SRAM(0xA000, 0xBFFF);
// ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM