[c++] 关于STL中list类的Remove方法的编译错误C2678

今天搜stl的资料时,无意看到csdn论坛一个关于代码的问题,我随意看了下下面的各种回复,我简直怒火中烧!或许我的挥发性太大,但是每个人都是新手过来的,一步一步,你在网上发表的言论不难看出你的素养和教养!

问题如下:

原问题链接: 点击

 

#include  <iostream>
#include <cstdlib>
#include <string>
#include <bitset>
#include <vector>
#include <iterator>
#include <list>
#include <unistd.h>
#include <istream>
#include <iomanip>
using namespace std;

struct student
{
char  *name;
char  *address;
char  *tel;
float score;

};

//我定义了一个结构体,然后定义了结构体list,向结构体list扔了4个元素,然后想remove掉其中的一
//个,结果在remove的时候报错了

int main(int argc,char *argv[])
{

student stu;
list<student> stulist;


stu.name="chenyigeng";
stu.address="hebei";
stu.tel="13811114116";
stu.score=1123.423;
stulist.push_back(stu);
stu.name="lijie";
stu.address="henan";
stu.tel="13411323116";
stu.score=123.423;
stulist.push_back(stu);

stu.name="xuhontao";
stu.address="shandong";
stu.tel="122311323116";
stu.score=1123.423;
stulist.push_back(stu);
stu.name="jianian";
stu.address="langfang";
stu.tel="13423323116";
stu.score=12334.423;
stulist.push_back(stu);
student s1={"lijie","henan","13411323116",123.423};
const student &ss=s1;
stulist.remove(ss); //这里编译不通过

system("pause");
return 0;
}

----------------------------------------------------------------------------------------------------

其实这个问题根据错误是很容易推断出来是什么问题, 我把源码复制过去,编译一下,vs提示错误如下:

++\vcpro\designpattern\compositepattern\test.cpp(104): error C2678: 二进制“==”: 没有找到接受“student”类型的左操作数的运算符(或没有可接受的转换)
3>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.10.25017\include\system_error(386): note: 可能是“bool std::operator ==(const std::error_condition &,const std::error_condition &) noexcept”
3>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.10.25017\include\system_error(378): note: 或    “bool std::operator ==(const std::error_condition &,const std::error_code &) noexcept”
3>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.10.25017\include\system_error(370): note: 或    “bool std::operator ==(const std::error_code &,const std::error_condition &) noexcept”
3>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.10.25017\include\system_error(362): note: 或    “bool std::operator ==(const std::error_code &,const std::error_code &) noexcept”
3>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.10.25017\include\exception(346): note: 或    “bool std::operator ==(const std::exception_ptr &,std::nullptr_t) throw()”
3>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.10.25017\include\exception(341): note: 或    “bool std::operator ==(std::nullptr_t,const std::exception_ptr &) throw()”
3>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.10.25017\include\exception(336): note: 或    “bool std::operator ==(const std::exception_ptr &,const std::exception_ptr &) throw()”
3>f:\c++\vcpro\designpattern\compositepattern\test.cpp(104): note: 尝试匹配参数列表“(student, student)”时
3>已完成生成项目“CompositePattern.vcxproj”的操作 - 失败。

很明显,是少了operator==, 在方法调用的时候,有些地方需要==操作;

我们再来看看 代码中所调用的 list的remove方法,参数是传入一个对旬,我们不需要知道这个方法是具体如何实现的,但是至少一点,传入的是一个结构,你要从一个list<Student>中删除一个Student元素,

很简单的就想到了,首先必须先找到要删除的Student,那如何找到,是不是要把传入的Student和List中的Student对象进行比较,那两个对象如何比较,是不是就有了 operator== ,由于在C++中,默认是没有operator==,需要我们手动写一个,

自己手写一个operator== ,编译就通过了;

------------------------------------------------------------------------------------------------------------------------------------------------

问题回答完毕,也在比记录此知识点;

顺便抨击一下现在的程序员或者某些会写那么两三句代码,甚至于比我水平还差的,在此请你低调点,要么你不会你就努力去学去查,要么你就闭嘴不要误导别人,或者在网上以打击别人来找存在感,你的存在不仅恶心了我,还恶心了整个世界!

下面是各种回复,简直要上天了:

 

 回复那个zhcosin : 

  “插都没插入链表,你还从链表删除它,不报错才怪。”  先不说,编译不通过和逻辑有什么关系,编译只是检查语法,你懂吗???这都不懂你也敢出来教训别人!

   “乱七八糟的,你那个 stu 怎么还多次插入链表了?”    怎么就乱七八糟了?   stu对象每次赋值完插入list,重新赋值再插入,哪里有问题?不知者无罪,但是你不懂还出来瞎喷,和疯狗有什么区别?在这个地方,list在插入stu的时候,list中并不是保存stu对象,而是另外一个通过stu浅拷贝出来的对象;

  ”应该查 STL 的用法,他连用法都没搞清楚,语法错误一堆,逻辑错误也是一堆,这是我见过的错误最多的程序了。“    我没觉得这位同学哪里的语法没有用好,相反,我觉得他比你强多了,用的好多了,不解释,参照上面即可看出,水平比你高不止一个档次!如果说这是你见过错误最多的程序,我敢                                               说你的水平根本没法拿出来见人!!!

    其实的就不一一回复,只是这个烂仔水平垃圾不说,还喜欢到处 误导别人,实在看不下去,真是一粒老鼠屎,坏了一窝汤!

 

转载于:https://www.cnblogs.com/matsn/p/7250697.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值