【问题标题】:Multidimensional arrays replacing values多维数组替换值
【发布时间】:2014-12-21 14:26:12
【问题描述】:

当我打印我的字段时它可以工作,但是当我更改一个值时,数组似乎被重置了。我想我在错误的地方声明了我的字符串veld[10][11],但我不舒尔。 也得到了 veld 作为我班级的一个属性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();
            }
    {
{

【问题讨论】:

  • 代码示例末尾的那些左大括号看起来很奇怪。
  • 成员变量veld 被同名的局部变量隐藏。为局部变量选择一个不同的名称,使用this-&gt;veld 访问成员变量,或者删除名为veld 的局部变量,因为它们似乎没有必要。

标签: c++ multidimensional-array


【解决方案1】:

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

问题

问题在于您在 spelbord::printVeld 函数中使用了 local 变量:

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. */
}

这一次成员只被初始化一次,当对象被构建时,你修改并显示这个成员。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    相关资源
    最近更新 更多