class 中“==”运算符的问题

Issue with "==" operator in a class

我在 class room 中的代码有问题。每当我尝试让房间构造函数(如下所示)在数组 ARoomADisc 中放置一个字符时,它与我试图使用 break all 忽略的字符相同当我使用 APrint() 时,值会被忽略。

room::room(int Si, int Sd)
{
    //makes ADisc and ARoom blank

    for(int d=1; d<=25; d++)
    {
        for(int i=1; i<=78; i++)
        {
            this->SetSpot(i,d, char(0), "0");
        }
    }
    //this makes some filled
    for(int i=1; i<=Si; i++)
    {
        for(int d=1; d<=Sd; d++)
        {
            this->SetSpot(i,d, ' ', "0");
        }
    }
}

SetSpot()

void SetSpot(int i, int d, char ch, string disc)
{
    int Ni = i-1;
    int Nd = d-1;
    this->ARoom[Nd][Ni]=ch;
    this->ADisc[Nd][Ni]=disc;
}

Aprint()

void Aprint()
{
    system("CLS");
    for(int d=0; d<25; d++)
    {
    if(this->ARoom[d][0]==char(0))
        break;
    cout << "[";
        for(int i=0; i<78; i++)
        {
            if(this->ARoom[i][d]==char(0))
                break;
            cout << this->ARoom[d][i];
        }
    cout << "]" << endl;
    }
    int r = 0;
    for(int d=0; d<25; d++)
    {
        for(int i=0; i<78; i++)
        {
            if(this->ADisc[d][i]=="0")
            {
                break;
            }
            else
            {
                r++;
                cout << "[" << r << "," << this->ARoom[d][i] << "]: " << this->ADisc[d][i] << endl;
            }
        }
    }
}

主要()

int main()
{
    room Spawn(10,10);
    Spawn.SetSpot(5,1, char(1), "me");
    Spawn.SetSpot(4,1, char(2), "you");
    Spawn.Aprint();
}

Returns

[   ☻☺     ]
[          ]
[          ]
[          ]
[          ]
[          ]
[          ]
[          ]
[          ]
[          ]

Process returned 0 (0x0)   execution time : 0.039 s
Press any key to continue.

如果我将 if(this->ADisc[d][i]=="0") 更改为 if(this->ADisc[d][i]=="me") 然后我得到这个

[   ☻☺     ]
[          ]
[          ]
[          ]
[          ]
[          ]
[          ]
[          ]
[          ]
[          ]
[1, ]: 0
[2, ]: 0
[3, ]: 0
[4,☻]: you
[5, ]: 0
[6, ]: 0
[7, ]: 0
[8, ]: 0
[9, ]: 0
[10, ]: 0
[11, ]: 0
[12, ]: 0
[13, ]: 0
[14, ]: 0
[15, ]: 0

它继续 [1876, ]: 0 但不打印 [3,☺]: me。 我该如何解决或绕过它?

您的问题与 "break" 的使用有关。 "break" 是一个打破当前循环构造的命令。

当您搜索“0”(空磁贴)时,它会在看到空白时立即调用 break,取消整行的其余打印。

当您搜索 "me"(玩家方块?)时,它会在看到 "me" 方块时调用 break,跳过 "me" 方块的打印并取消其余方块的打印那一行,所以任何没有 "me" 的行都可以。

所以在这里去掉 break 就可以避免这个问题。

~~~

但是,您提到它不打印您试图忽略的字符,但问题是您的 if 语句明确告诉它 打印图块在这些情况下的信息,所以很难知道你在这里的意图是什么。也许您可以尝试不使用 if 语句,如果还有任何遗留问题,请告诉我们。