Galaxy星 2021-07-22 23:05 采纳率: 100%
浏览 20
已采纳

循环(for语句)输出中,it->m_Name前面的左移运算符为什么会报错,请放到编译器中调试


#include<iostream>
#include <set>
#include <string>
using namespace std;
//set容器排序 存放自定义数据类型
class Person
{
public:
    Person(string name, int age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }
    string m_Name;
    int m_Age;
};
class comparePerson
{
public:
    bool operator()(const Person& p1, const Person& p2)
    {
        //按照年龄进行降序
        return p1.m_Age > p2.m_Age;
    }
};
void test01()
{
    set<Person,comparePerson> s;
    //创建Person对象
    Person p1("刘备", 24);
    Person p2("关羽", 28);
    Person p3("张飞", 25);
    Person p4("赵云", 21);

    s.insert(p1);
    s.insert(p2);
    s.insert(p3);
    s.insert(p4);

    for (set<Person,comparePerson>::iterator it = s.begin();it != s.end();it++)
    {
        cout < "姓名: " << it->m_Name << "年龄: " << it->m_Age;
    }

}

int main()
{
    test01();
}

![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/885162669626154.png)



img

  • 写回答

1条回答 默认 最新

  • 八云黧 2021-07-22 23:06
    关注

    因为你第一个左移少写了一个<,编译器把前面认作是表达式,所以在第二个左移运算符处报错,因为它左边不是cout而是一个布尔值

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 7月22日
  • 创建了问题 7月22日

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM