【发布时间】:2017-01-17 02:13:43
【问题描述】:
我最近收到标题错误,我不太确定我做错了什么,有人有什么想法吗?相关代码如下
Rom.h
//Rom.h
#pragma once
#include <fstream>
struct ROM {
uint8_t* memblock;
std::fstream rom;
std::streampos size;
int flag;
void ROM::LoadROM(std::string path, int flag);
void ROM::BinaryDump(std::string path, std::streampos size);
}
Rom.cpp
//Rom.cpp
#include <iostream>
#include "Rom.h"
void ROM::LoadROM(std::string path, int flag) {
this->rom.open(path, std::ios::in | std::ios::binary | std::ios::ate);
if (rom.is_open()) {
this->size = rom.tellg();
std::cout << "\nThe Rom is " << rom.tellg() << " byte(s) long." << std::endl;
this->memblock = new uint8_t[(unsigned int)this->size];
std::cout << rom.tellg() << " Bytes of memory have been allocated" << std::endl;
rom.seekg(0, std::ios::beg);
rom.read(this->memblock, (unsigned int)this->size);
std::cout << "The contents of the file are stored in memory" << std::endl;
rom.close();
if (flag == 0) {
}
else {
BinaryDump(path, this->size);
}
}
}
void ROM::BinaryDump(std::string path, std::streampos size) {
std::fstream binDump(path, std::ios::out | std::ios::binary);
binDump.write(this->memblock, size);
delete[] this->memblock;
binDump.close();
std::cout << "The ROM has been dumped" << std::endl;
}
对不起,如果这很明显,但我在这里感到迷茫。
【问题讨论】:
-
编译器通常会告诉您发生错误的行。使用武力,年轻的学徒 :-)
-
请显示准确的错误信息并指出它对应的源代码行
-
与问题无关,但您的班级违反了三原则。要解决此问题,请使用
vector<uint8_t>而不是uint8_t *。 -
@M.M,我不确定这对 RO3/5 有何违规/修复。并不是说使用向量不是一个好主意,但我认为这与确保定义
rom类的所有 3/5 方法(如果您定义一个)是分开的。 -
@paxdiablo 我的意思是在某些情况下分配或复制对象时会泄漏内存
标签: c++ char type-conversion uint8t