【问题标题】:argument of type uint8_t is incompatible with parameter of type char*uint8_t 类型的参数与 char* 类型的参数不兼容
【发布时间】: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&lt;uint8_t&gt; 而不是 uint8_t *
  • @M.M,我不确定这对 RO3/5 有何违规/修复。并不是说使用向量不是一个好主意,但我认为这与确保定义 rom 类的所有 3/5 方法(如果您定义一个)是分开的。
  • @paxdiablo 我的意思是在某些情况下分配或复制对象时会泄漏内存

标签: c++ char type-conversion uint8t


【解决方案1】:

uint8_tchar 是不同的类型。 (嗯 - 这取决于系统,但在您的系统上它们是不同的)。这两个名称不能互换使用。

您的问题没有提到是哪一行给出了错误,但是如果您查看它,您会发现您正在传递uint8_t *,实际上该函数需要char *

我猜应该是 rom.read 。如果是这样,那么您可以通过以下方式解决问题:

rom.read( reinterpret_cast<char *>(memblock), size );

您不需要使用多余的 unsigned int 转换或 this-&gt; 前缀。

另一种可能的解决方案是使memblock 具有char * 类型。

【讨论】:

  • reinterpret_cast&lt;char *&gt;(memblock)
  • @deW1 是的,它会很安全
  • 拍,完全忘了给行号,谢谢!
猜你喜欢
  • 2012-11-06
  • 1970-01-01
  • 2019-12-16
  • 2016-05-19
  • 1970-01-01
  • 2021-02-21
  • 1970-01-01
  • 2020-01-20
  • 1970-01-01
相关资源
最近更新 更多