错误左值需要作为赋值c ++的左操作数

Error lvalue required as left operand of assignment c++(错误左值需要作为赋值c ++的左操作数)
本文介绍了错误左值需要作为赋值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 ++的左操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)