c++ - C++ 中的结构错误

标签 c++ header struct

尝试在 C++(visual studio)中执行此操作时,我遇到了结构问题:

内存.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++ 给我这个错误:

1>memoria.cpp(75): error C2065: 'writeRAMBankEnable': 未声明的标识符

1>memoria.cpp(76): error C2065: 'writeROMBankSelect': 未声明的标识符

1>memoria.cpp(77): error C2065: 'writeRAMROMModeSelect': 未声明的标识符

1>memoria.cpp(78): error C2065: 'writeVRAM' : 未声明的标识符

1>memoria.cpp(79): error C2065: 'writeSRAM' : 未声明的标识符

1>memoria.cpp(80): error C2065: 'writeRAM': 未声明的标识符

1>memoria.cpp(81): error C2065: 'writeERAM' : 未声明的标识符

1>memoria.cpp(82): error C2065: 'writeSprite' : 未声明的标识符

1>memoria.cpp(83): error C2065: 'writeIOM': 未声明的标识符

1>memoria.cpp(84): error C2065: 'writeHRAM': 未声明的标识符

1>memoria.cpp(169): error C2065: 'implReadMem': 未声明的标识符

1>memoria.cpp(179): error C2065: 'implReadMem': 未声明的标识符

为了记录,我已经在我的 memoria.h 中声明了所有函数,当然除了结构和 implWriteMem[]。 无论如何,我该如何解决?

附言它在纯 C 中运行良好。

谢谢!

最佳答案

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

你的意思是

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

在引用成员时必须包括类名,类内除外。并且在创建指向成员函数的指针时,您始终需要类名。

最好的解决方案是使用语言内置的多态特性:

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);
};

等等。然后你可以创建一个基类型的句柄数组,并填充各种特定于行为的类:

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

关于c++ - C++ 中的结构错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9441561/

相关文章:

c++ - 我想在 opengl 中将图像包裹在一个球体周围

c++ - typeid,如何仅获取类型名称

html - 我的 Logo 不会出现在页眉上,正确的文件路径是什么?

c++ - 链表数组大小不同

c++ - 告诉子类对父类(super class)的 protected 变量做某事是一种好习惯(也许是一些已知的设计模式?)?

c++ - 在 OpenCV 中将 vector<Point3f> 转换为 CvMat*?

html - 标题下的边栏和正文 "hiding"

c++ - 编译多个 C++ OpenGL

json - Go json unmarshal 不获取单个属性值

C代码冒泡排序有时不能给出正确的输出