【问题标题】:Convert char to string doesn't work fine C++将 char 转换为字符串不能正常工作 C++
【发布时间】:2013-09-20 20:21:16
【问题描述】:

我有那个代码:

for (int x = 0; x<(worldWidth-1); x++) {
    for (int y = 0; y<(worldHeight-1); y++) {
        sf::Texture texture;
        if (!texture.loadFromFile("images/blocks/" + ruta(mapa[y][x]) + ".png"))
        return -1;

        sf::RectangleShape rectCaja(sf::Vector2f(16, 16));
        rectCaja.setPosition(sf::Vector2f(x*16, y*16));
        rectCaja.setTexture(&texture);
        window.draw(rectCaja);
    }
}

打印框(16*16 像素),在游戏中是“块”,问题是它不打印任何块,它直接崩溃我不知道为什么:/

我知道(通过控制台测试)数组“mapa”没有错......所以我唯一的解释是 ruta 函数不能正常工作......(我已经用标准测试了它: :string var = "dirt"; 它工作正常)... :/

std::string ruta(char id) {

if (id=='0') return "air";
if (id=='1') return "stone";
if (id=='2') return "dirt";
if (id=='3') return "grass_side";
if (id=='4') return "coal_ore";

}

如果有人想要保留代码:http://pastebin.com/5jvbzwkR

谢谢! :P

【问题讨论】:

  • 您将 int id 与 char 进行比较
  • 如果id 不是这些东西怎么办? (你有没有收到任何编译器警告?)
  • 这段代码效率很低,因为每次调用 ruta() 它都会创建一个新的字符串来返回。您最好创建一个数组 const std::string ruta[] = { "air", "stone", "dirt", "grass_side", "coal_ore" }; 并使用 ruta[mapa[y][x]]。

标签: c++ arrays char sfml


【解决方案1】:

只是猜测,因为没有足够的信息可以确定,但这可能就是答案

std::string ruta(int id) {

if (id==0) return "air";
if (id==1) return "stone";
if (id==2) return "dirt";
if (id==3) return "grass_side";
if (id==4) return "coal_ore";

}

在 C++ 中,您必须小心类型,并了解intchar 之间的区别。值为 '3' 的 char 与值为 3 的 an int 不同。

【讨论】:

  • 好吧,我忘记在函数参数中将 'int' 更改为 'char' 但它仍然崩溃,我不知道为什么...:/
  • 这是错误的if (!texture.loadFromFile("images/blocks/" + ruta(mapa[y][x]) + ".png")) 应该是if (!texture.loadFromFile("images/blocks/" + ruta(mapa[x][y]) + ".png"))。你弄错了 x 和 y。
  • 这个char mapa[worldWidth-1][worldHeight-1]; 看起来不对。为什么不是显而易见的char mapa[worldWidth][worldHeight];?你可能有一个很好的理由,但我怀疑这只是一个错误。
  • @Ikillnukes 不,我没有说相同的代码,你把 x 和 y 弄错了
  • @Ikillnukes 您的代码中可能有很多错误。您应该使用调试器。
【解决方案2】:

我立即看到的一个问题是您将intchar 进行比较。考虑:

std::string ruta(int id)
{
    switch( id )
    {
    case 0:
        return "air";
    case 1:
        return "stone";
    case 2:
        return "dirt";
    case 3:
        return "grass_side";
    case 4:
        return "coal_ore";
    }
}

【讨论】:

  • 啊!我忘记输入 char id,但它仍然崩溃...:/
【解决方案3】:

这是你的场景声明:

int scene[worldWidth][worldHeight]; 

以下是填充场景的方法:

while (!finished) {
    if (yPos >= topOfTheWorld) {
        scene[xPos][yPos] = 1;
    } 
    else if(yPos < topOfTheWorld) {
        scene[xPos][yPos] = 0;
    }

    //etc...
}

以下是您写入 mapa.txt 的方式:

std::ofstream output("mapa.txt");
for(int y=0;y<worldHeight;y++) {
    for(int x=0;x<worldWidth;x++) {
        output<<scene[x][y];

        if(x<(worldWidth-1)){output<<",";}
    }
    if(y<(worldHeight-1)){output<<std::endl;}
}

基本上,这意味着您将数值 0 和 1 写入 mapa.txt,而不是字符值 '0' 和 '1'。然而,在你的 ruta 函数中,你与“0”和“1”进行比较。您应该与不带单引号 (') 的 0 和 1 进行比较。

【讨论】:

  • 我写的是你说的,但是当我为所有地图充电时,它以 char 而不是 int 的形式充电,所以...我必须将它作为 char 进行比较,而不是作为一个int ...但如果我这样做,应用程序仍然崩溃...:/
猜你喜欢
  • 2010-12-04
  • 2018-03-27
  • 2013-05-13
  • 2012-01-16
  • 1970-01-01
  • 2018-09-02
  • 2010-11-22
  • 1970-01-01
  • 2020-05-20
相关资源
最近更新 更多