gpt4 book ai didi

c++ - 错误 char 和 const char

转载 作者:行者123 更新时间:2023-11-30 02:50:07 24 4
gpt4 key购买 nike

我对编程很陌生,我编写的代码让我感到困惑。你能解释一下以下错误吗?

int getBill(char seat)// a return function.
{
int b=0;
char FS, fs, LBS, lbs, UBS, ubs, GPS, gps;
char seat;
if(seat=="FS"||seat=="fs")
b=15000;
if(seat=="LBS"||seat=="lbs")
b=10000;
if(seat=="UBS"||seat=="ubs")
b=5000;
if(seat=="GPS"||seat=="gps")
b=1500;

return b;
}

错误:操作数类型不兼容(char 和 const char)

最佳答案

更改您的函数以获取 std::string 而不是单个 char。并删除多余的 char 声明。

int getBill(const std::string &seat)// a return function.
{
int b=0;

if(seat=="FS"||seat=="fs")
....

请注意它是 usually如上所示,通过 const 引用传递结构的效率要高得多。

关于c++ - 错误 char 和 const char,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20704073/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com