错误左值需要作为赋值c ++的左操作数 [英] Error lvalue required as left operand of assignment c++

查看:45
本文介绍了错误左值需要作为赋值c ++的左操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

整个程序基本上只允许用户移动光标,如果用户在给定的坐标范围 (2,2) 内,则允许用户键入输入.我刚刚提供了一些我认为足以解决问题的代码.

The whole program basically just allows the user to move the cursor and if the user is in the given range of coordinates (2,2), the user is allowed to type the input. I have just provided some bits of the code which I thought would be enough for solving the problem.

我不知道是什么导致了这个问题.你能解释一下为什么会这样吗?

I don't know what is causing this problem.Can you also explain why is it happening !!

void goToXY(int ,int);

用两个整数创建了一个函数.

Created a function with two ints.

int X = 0, Y = 0;

初始化两个整数.

if(X = 2 && Y = 2){
    cin >> input;
}

这是错误所在(在上面)

This is where the error is(it's above)

void goToXY(int x = 0, int y = 0) {

    COORD c;
    c.X = x;
    c.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

这里是我定义函数的地方(在上面)

Here's where I define the function(it's above)

推荐答案

问题出在这个if语句

if(X = 2 && Y = 2){
    cin >> input;
}

条件被解释为

if(X = ( 2 && Y ) = 2){
    cin >> input;
}

我想你是说

if(X == 2 && Y == 2){
    cin >> input;
}

考虑到最好在函数声明而不是函数定义中包含默认参数,因为通常它是在编译单元中可见的声明.

Take into account that it is better to include default arguments in the function declaration instead of the function definition because usually it is the declaration that is visible in a compilation unit.

例如

void goToXY( int = 0, int = 0 );

您也可以在提供另一个默认参数的块作用域中重新声明函数.

Also you may redeclare the function in a block scope supplying another default arguments.

这篇关于错误左值需要作为赋值c ++的左操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆