无法在函数main中编译,需要Lvalue
Cannot compile, Lvalue required, in function main
我想编译这个,但它给了我错误,即使我更改了引号,头文件中是否有任何错误,请告诉我
#include<iostream.h>
#include<conio.h>
void main()
{
char st[20];
cin>>st;
cout<<st<<endl;
if (st = 'a')
cout<<"This is a";
if (st = 'b')
cout<<"This is b";
getch();
}
以下内容不太正确:
if (st = 'a')
if (st = 'b')
首先,=
是赋值,而不是比较。第二,CCD_ 2和CCD_ 3是CCD_。
写以上内容的正确方法是
if (strcmp(st, "a") == 0)
if (strcmp(st, "b") == 0)
也就是说,我鼓励您不再使用C字符串,而是使用std::string
。
=
不用于比较,
if (st = 'a')
if (st = 'b')
它将尝试改变st
,并且以上比较的结果总是true
。
尝试使用std::string
:
#include <string>
...
std::string st;
std::cin >> st;
cout<<st<<endl;
if (st == "a")
cout<<"This is a";
if (st == "b")
cout<<"This is b";
if (st = 'a')
if (st = 'b')
在上面的两行中,l-value(左值('st'都指向数组的开头,并且不能更改其地址。这就是为什么在编译中会出现l-value错误的原因。使用相等(==(运算符而不是赋值(=(和取消引用st来更改If条件,以在开头获取值。
if (*st == 'a')
if (*st == 'b')
好吧,在我的学习线上。您正在使用赋值运算符"="导入string.h指令,并使用该库的strcmp();
函数希望这对有帮助
相关文章:
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 热键/按钮,根据需要进行编译,但不运行(在F5和Ctrl+Shift+B之间)
- 如何摆脱为条件编译定义预处理器宏的需要?
- 模板需要编译时常量值
- 如何在不需要LIBCD.lib的情况下在Visual Studio 6中编译C项目
- 使用 C++17 编译特征需要_SILENCE_CXX17_NEGATORS_DEPRECATION_WARNING定义
- 从DLL导出函数,LoadLibrary()需要用TEXT转换的字符串才能编译而不会出错
- 为什么我需要包含<compare>标头才能编译 <=>?
- 编译以下重载<<需要什么enable_if或其他提示?
- 为什么clang仍然需要libgcc.a来编译我的代码
- 在 Flashdevelopment OpenFL 中编译需要 31 分钟
- 为什么这段代码需要这么长时间才能用 g++ 编译?
- 快速搜索链表未编译,需要返回语句
- 如何编译需要 <winsock2.h> 的 c++ 项目?
- 用调试符号编译需要其他库
- 在VS2010中编译需要太长时间
- 在安卓上编译需要 stl 的库
- nvcc无法在调试模式下编译:需要单个文件
- 无法编译需要 C99 编译器 (AFAIU) 的 pyethash python 包。错误 - 无法打开包含文件:"alloca.h"
- 编译需要 #include "stdafx.h"