문제

I have the following code in C++:

void main()
{
string line_1="1 | 2 | 3";
string line_2="4 | 5 | 6";
string line_3="7 | 8 | 9";
int choice;

cin>>choice;

switch (choice)
            {
            case 1:
                replace(line_1.begin(), line_1.end(), "1", "O");
                break;
            case 2:
                replace(line_1.begin(), line_1.end(), "2", "O");
                break;
            case 3:
                replace(line_1.begin(), line_1.end(), "3", "O");
                break;
            case 4:
                replace(line_2.begin(), line_2.end(), "4", "O");
                break;
            case 5:
                replace(line_2.begin(), line_2.end(), "5", "O");
                break;
            case 6:
                replace(line_2.begin(), line_2.end(), "6", "O");
                break;
            case 7:
                replace(line_3.begin(), line_3.end(), "7", "O");
                break;
            case 8:
                replace(line_3.begin(), line_3.end(), "8", "O");
                break;
            case 9:
                replace(line_3.begin(), line_3.end(), "9", "O");
                break;
            {
            default:
                break;
            }
            }
    }

What it does is that it changes the lines given above to inputs of tic-tac-toe, in this case O. But when I run this code I get these three errors:

ERROR C2446: '==': no conversion from 'const char' to 'int'
ERROR C2040: '==': 'int' differs in levels of indirection from 'const char[2]'
ERROR C2440: '=': cannot convert from 'const char[2]' to 'char'.

What could possibly be going wrong with my code?

도움이 되었습니까?

해결책

Try:

replace(line_1.begin(), line_1.end(), '1', 'O');

다른 팁

Try:

replace(line_1.begin(), line_1.end(), '1', 'O');
//                                    ^^^^^^^^^

Suggest you call replace member function from std::string:

line_1.replace(line_1.begin(), line_1.end(), '1', 'O');
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top