c ++在方法中更改局部变量

时间:2017-10-04 07:00:41

标签: c++ methods tetris

你好我正在用ascii输出写一个基本的俄罗斯方块

我有一个我想要移动的块对象

如何从我的方法中更改字段中某个位置的角色?

但VisualStudio告诉我

1>------ Build started: Project: Tetris, Configuration: Debug Win32 ------
1>Tetris.cpp
1>e:\dateien\uni\c++\tetris\tetris\tetris.cpp(48): warning C4244: '=': conversion from 'int' to 'float', possible loss of data
1>e:\dateien\uni\c++\tetris\tetris\tetris.cpp(49): warning C4244: '=': conversion from 'int' to 'float', possible loss of data
1>e:\dateien\uni\c++\tetris\tetris\tetris.cpp(50): error C2108: subscript is not of integral type
1>e:\dateien\uni\c++\tetris\tetris\tetris.cpp(91): warning C4305: '+=': truncation from 'double' to 'float'
1>Done building project "Tetris.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

代码:

#include <iostream>

using namespace std;

int main()
{

const int FieldY = 20;
const int FieldX = 11;

char TetrisField[FieldY][FieldX] =
{
    "__________",
    "__________",
    "__________",
    "__________",
    "__________",
    "__________",
    "__________",
    "__________",
    "__________",
    "__________",
    "__________",
    "__________",
    "__________",
    "__________",
    "__________",
    "__________",
    "__________",
    "__________",
    "__________",
    "__________",
};

class Block {
public:
    float posX;
    float posY;
    char Character = 'A';

    void setPos( int x, int y)
    {
        posX = x;
        posY = y;
        TetrisField[posY][posX] = Character;
    };
};



while (true)
{
    Block B1;
    B1.setPos(0, 5);

    for (int n = 0; n < FieldY; n = n + 1)
    {
        cout << TetrisField[n][0] << TetrisField[n][1] << TetrisField[n][2] << TetrisField[n][3] << TetrisField[n][4]
            << TetrisField[n][5] << TetrisField[n][6] << TetrisField[n][7] << TetrisField[n][8] << TetrisField[n][9] << endl;

    };

};
return 0;
};

我知道输出方法很糟糕,但那只是为了测试

好吧,现在我写了随机的东西,以便堆栈溢出编辑器允许我发布这个问题,因为显然我没有写过enaugh正常的单词,这不是代码。啊终于啊; - )

THX

1 个答案:

答案 0 :(得分:0)

TetrisField[posY][posX] posYposX是浮点值。 &#34;下标不是整数类型&#34;表示您无法使用它们索引数组。