替换值的多维数组

Multidimensional arrays replacing values

本文关键字:数组 替换      更新时间:2023-10-16

当我打印我的字段时,它可以工作,但是当我更改值时,数组似乎被重置了。我想我把我的字符串veld[10][11]在错误的地方,但我不是舒尔。还得到了草原作为我班级speelveld.h的属性谢谢

    #include "speelveld.h"
    #include "Schip.h"
    void spelbord::printVeld(){
        //spelbord::zetBoot();
        string veld[10][11]= {
            { "A", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
            { "B", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
            { "C", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
            { "D", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
            { "E", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
            { "F", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
            { "G", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
            { "H", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
            { "I", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
            { "J", " ", " ", " ", " ", " ", " ", " ", " ", " ", " " }
        };
        cout << "  1 2 3 4 5 6 7 8 9 10" << endl;
        for (int i = 0; i < 10; i++){
            cout << " +-+-+-+-+-+-+-+-+-+-+" << endl;
            for (int j = 0; j < 11; j++){
                cout << veld[i][j] << "|" << flush;
            }
            cout << endl;
        }
        cout << " +-+-+-+-+-+-+-+-+-+-+" << endl;
    }
    void spelbord::zetBoot(){
        string veld[10][11];
        cout << "Wat is de eerste coordinaat van je vliegdekschip? (bv: a1) " << flush;
        cin >> vliegdekschip1;
        y = vliegdekschip1[0];
        x = vliegdekschip1[1];
        cout << y << endl;
        cout << x << endl;
        spelbord::checkpos();
    }
    void spelbord::checkpos(){
        if (y == 97){
            if (x == 49){
                veld[0][1] = "O";
                spelbord::printVeld();
            }
    {
{

对于我的其余答案,我假设您的类spelbord具有类型为string的属性veld

问题所在

问题是您在spelbord::printVeld函数中使用了局部变量:

void spelbord::printVeld()
{
    /* Initialize a 'new' local variable each time you pass here. */
    string veld[10][11] = {
        /* Initialization value. */
    }
    /* Print header. */
    for (int i = 0; i < 10; ++i)
    {
        for (int j = 0; j < 11; ++j)
            /* Refers to the variable just initialized. */
            cout << veld[i][j] << "|" << flush;
        cout << endl;
    }
    /* Print footer. */
}
void spelbord::checkpos()
{
    if (y == 97)
        if (x == 49)
        {
            /* Refers to the attribute 'veld' of the 'spelbord' object */
            veld[0][1] = "O";
            spelbord::printVeld();
        }
}

总而言之,您始终显示一个新初始化的变量。不是您在spelbord::checkpos中修改的那个.

可能的解决方案

/* Constructor. Used to initialize members. */
spelbord::spelbord()
{
    /* I don't use the constructor's prefered way of initialization because your 
       initialization value is huge. */
    veld = { /* Initialization. */ };
}
/* No local variable this time. */
void spelbord::printVeld()
{
    /* Print header. */
    for (int i = 0; i < 10; ++i)
    {
        for (int j = 0; j < 11; ++j)
            /* Refers to the member of the object. */
            cout << veld[i][j] << "|" << flush;
        cout << endl;
    }
    /* Print footer. */
}
void spelbord::checkpos()
{
    /* Same as before. */
}

这次成员仅在生成对象时初始化一次,并且您修改并显示此成员。