【发布时间】:2021-05-27 12:05:42
【问题描述】:
我正在尝试编写一个程序来读取姓氏介绍并返回学生的信息(生日、组等)。编译器不识别 == 运算符,我猜它不知道要比较什么,无论是地址还是值?(我会解释一下)
我读过类似的案例,他们建议使用 bool 运算符。据我所知,布尔运算符只返回真或假(我没有看到使用它来打印学生信息的方法)
我也在考虑使用 char 或 strcmp(用于比较的 srting 函数)。
void find_fam ( struct st student[], int length, string & fam)
{
for(int i=0; i<length; i++)
if ( student[i]== fam[i]){
cout << "Student:\n"<< student[i].imia << student[i].otchestvo << student[i].familia <<
student[i].gruppa << student[i].grozhdenia);
}
}
【问题讨论】:
-
您能否edit 您的问题并显示minimal reproducible example 任何人都可以完全如图所示 剪切/粘贴,然后编译并获取您的编译错误?显示的内容不足。
-
你很可能想要
if ( student[i].familia == fam)而不是if ( student[i]== fam[i]) -
在 C++ 中,您不需要在结构的使用前加上
struct,这是隐式的。 -
您将 std::string 与 struct 进行比较有点奇怪,这是绝对错误的。
-
提示:使用
std::vector<st>和for (auto&& student : students),你的论点看起来像const std::vector<st>& students。 C++ 有很多工具可以让你的生活更轻松,比如不必传递单独的长度参数,或者不必敲打student[i]直到你的手指掉下来。
标签: c++ string if-statement parameters operator-overloading