Error: no match for 'operator&&'

2019-09-15 02:56发布

问题:

I'm getting the following error

error: no match for 'operator&&' in 'board[0][0] && board[0][1]'

My code is the following:

    #include <iostream>
    #include <cstdlib>
    #include <ctime>

    using namespace std;

    int main()
    {
        string board[3][3];
         board[0][0] = "[ ]";
         board[0][1] = "[ ]";
         board[0][2] = "[ ]";
         board[1][0] = "[ ]";
         board[1][1] = "[ ]";
         board[1][2] = "[ ]";
         board[2][0] = "[ ]";
         board[2][1] = "[ ]";
         board[2][2] = "[ ]";
         string choice;
         int counter;
         do
         {
            for(counter=0;counter<5;counter++)
            {
                cout<<board[0][0]<<board[0][1]<<board[0][2]<<endl;
                cout<<board[1][0]<<board[1][1]<<board[1][2]<<endl;
                cout<<board[2][0]<<board[2][1]<<board[2][2]<<endl;
            cout<<"(Player 1) Tell me the coordinates of where you want your X togo: ";
                cin>>choice;
                cout<<endl;
                if(choice=="1,1")
                {
                    board[0][0] = "[X]";
                }
                else if(choice=="1,2")
                {
                    board[1][0] = "[X]";
                }
        else if(choice=="1,3")
        {
            board[2][0] = "[X]";
        }
        else if(choice=="2,1")
        {
            board[0][1] = "[X]";
        }
        else if(choice=="2,2")
        {
            board[1][1] = "[X]";
        }
        else if(choice=="2,3")
        {
            board[2][1] = "[X]";
        }
        else if(choice=="3,1")
        {
            board[0][2] = "[X]";
        }
        else if(choice=="3,2")
        {
            board[1][2] = "[X]";
        }
        else if(choice=="3,3")
        {
            board[2][2] = "[X]";
        }

        if(board[0][0] && board[0][1] && board[0][2] == "[X]" || board[1][0] && board[1][1] && board[1][2] == "[X]" || board[2][0] && board[2][1] && board[2][2] == "[X]" || board[0][0] && board[1][0] && board[2][0] == "[X]" || board[0][1] && board[1][1] && board[2][1] == "[X]" || board[0][2] && board[1][2] && board[2][2] == "[X]" || board[0][0] && board[1][1] && board[2][2] == "[X]" ||board[2][0] && board[1][1] && board[0][2] == "[X]" ||)
        {
            cout<<"Player 1 wins!"<<endl;
        }

    }
 }
 while(counter<5);
 return 0;

}

The line the error is on is the following:

if(board[0][0] && board[0][1] && board[0][2] == "[X]" || board[1][0] && board[1][1] &&     board[1][2] == "[X]" || board[2][0] && board[2][1] && board[2][2] == "[X]" || board[0][0]    && board[1][0] && board[2][0] == "[X]" || board[0][1] && board[1][1] && board[2][1] == "[X]" || board[0][2] && board[1][2] && board[2][2] == "[X]" || board[0][0] && board[1][1] && board[2][2] == "[X]" ||board[2][0] && board[1][1] && board[0][2] == "[X]" ||)

回答1:

You need to change something like:

board[0][0] && board[0][1] && board[0][2] == "[X]"

To:

board[0][0] == "[X]" && board[0][1] == "[X]" && board[0][2] == "[X]"

So that the inputs on each side of the && are boolean values



回答2:

I would recommend using an array of chars instead of an array of strings - then, when you ask the user for input and he enters say "1, 3" - you can simply either declare two ints and use them:

int x, y;
scanf("%i,%i", &x, &y);
board[x][y] = 'X'; 
// and when printing the board, use: cout << "["<<board[x][y]<<"]"; 
// - or better yet, use printf :) 

// alternatively with cin:
int x, y;
cin >> x;
cin.ignore(1,',');
cin >> y;
board[x][y] = 'X';

That would shorten up all of the "determine-which-square-the-user-picked" code :)