这是代码的一部分,我很麻烦
#include<iostream>
#include<fstream>
#include<conio.h>
#include<list>
#include<iterator>
#include<map>
#include<algorithm>
#include<ctime>
#include<sstream>
#include<vector>
#include<numeric>
#include<string>
using namespace std;
class C_Person
{
protected:
string M_SName;
string M_SEGN;
public:
inline C_Person& operator=(const C_Person& c_p) {
this->M_SName = c_p.M_SName;
this->M_SEGN = c_p.M_SEGN;
return *this;
}
};
class C_Student :public C_Person
{
public:
C_Student() {};
C_Student(const string Name, const string EGN, const string FN, const map<string, unsigned> st_book)
{
this->SetName(Name);
this->SetEGN(EGN);
this->SetFN(FN);
this->Student_Book = st_book;
}
C_Student(const C_Student &stud)
{
this->SetName(stud.GetName());
this->SetEGN(stud.GetEGN());
this->M_SFacNum = stud.M_SFacNum;
this->Student_Book = stud.Student_Book;
}
C_Student operator=(const C_Student&st) {
this->M_SName = st.M_SName;
this->M_SEGN = st.M_SEGN;
this->M_SFacNum = st.M_SFacNum;
}
private:
string M_SFacNum;
map <string, unsigned> Student_Book;
};
class C_Speciality
{
public:
C_Speciality(string spec, vector<C_Student>Vec_St) :
M_SSpec(spec),
V_Students(Vec_St)
{}
C_Speciality(const string &filename)//3.1*
{
try {
ifstream file(filename);
if (!file)
{
exception e;
throw e;
}
file >> this->M_SSpec;
copy(istream_iterator<C_Student>(file), istream_iterator<C_Student>(), back_inserter(this->V_Students));
file.close();
}
catch (exception e)
{
cout << "File did not open!";
}
}
list<C_Student*> GetStudentsWithDegeree(string const disciplineName, unsigned const degree) {//3.3*
list<C_Student*> listt;
copy_if(V_Students.begin(), V_Students.end(), back_inserter(listt),
[=](C_Student&st) {return st.GetDegreeByDisciplineName(disciplineName) == degree; });
return listt;
}
private:
string M_SSpec;
vector<C_Student> V_Students;
};
然后
copy_if(V_Students.begin(), V_Students.end(), back_inserter(listt),
[=](C_Student&st) {return st.GetDegreeByDisciplineName(disciplineName) == degree; }
);
它给我
Error C2679 binary '=': no operator found which takes a right-hand operand of type 'C_Student' (or there is no acceptable conversion) C++_echo c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\algorithm 585
或完整的输出
c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\algorithm(585): error C2679: binary '=': no operator found which takes a right-hand operand of type 'C_Student' (or there is no acceptable conversion)
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xutility(4233): note: could be 'std::back_insert_iterator<std::list<C_Student *,std::allocator<_Ty>>> &std::back_insert_iterator<std::list<_Ty,std::allocator<_Ty>>>::operator =(std::back_insert_iterator<std::list<_Ty,std::allocator<_Ty>>> &&)'
1> with
1> [
1> _Ty=C_Student *
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xutility(4233): note: or 'std::back_insert_iterator<std::list<C_Student *,std::allocator<_Ty>>> &std::back_insert_iterator<std::list<_Ty,std::allocator<_Ty>>>::operator =(const std::back_insert_iterator<std::list<_Ty,std::allocator<_Ty>>> &)'
1> with
1> [
1> _Ty=C_Student *
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xutility(4210): note: or 'std::back_insert_iterator<std::list<C_Student *,std::allocator<_Ty>>> &std::back_insert_iterator<std::list<_Ty,std::allocator<_Ty>>>::operator =(C_Student *&&)'
1> with
1> [
1> _Ty=C_Student *
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xutility(4204): note: or 'std::back_insert_iterator<std::list<C_Student *,std::allocator<_Ty>>> &std::back_insert_iterator<std::list<_Ty,std::allocator<_Ty>>>::operator =(C_Student *const &)'
1> with
1> [
1> _Ty=C_Student *
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\algorithm(585): note: while trying to match the argument list '(std::back_insert_iterator<std::list<C_Student *,std::allocator<_Ty>>>, C_Student)'
1> with
1> [
1> _Ty=C_Student *
1> ]
1>c:\users\echo\source\repos\c++_lubo_kursova\c++_lubo_kursova\source.cpp(304): note: see reference to function template instantiation '_OutIt std::copy_if<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>,std::back_insert_iterator<std::list<C_Student *,std::allocator<C_Student*>>>,C_Speciality::GetStudentsWithDegeree::<lambda_3c3836fbaa0c70067d1b37845af861f2>>(_InIt,_InIt,_OutIt,_Pr)' being compiled
1> with
1> [
1> _OutIt=std::back_insert_iterator<std::list<C_Student *,std::allocator<C_Student *>>>,
1> _Ty=C_Student,
1> _InIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<C_Student>>>,
1> _Pr=C_Speciality::GetStudentsWithDegeree::<lambda_3c3836fbaa0c70067d1b37845af861f2>
1> ]
1>Done building project "C++_echo.vcxproj" -- FAILED.
我可以看到没有使用C_Student的operator =,但是我在做什么错呢?仅仅是操作员超负荷吗?我也想问一下lambda是否在这里正确使用。我应该只用比较器代替它吗(我一次使用它)?
最佳答案
在您的GetStudentsWithDegeree
中,您试图从V_Students
的向量中获取一个对象并将其放入list
的C_Student*
中,即指向对象的指针。这是正确的成员函数代码:
list<C_Student> GetStudentsWithDegeree(string const disciplineName, unsigned const degree) {
list<C_Student> listt;
copy_if(V_Students.begin(), V_Students.end(), back_inserter(listt),
[=] (const C_Student &st) {
return st.GetDegreeByDisciplineName(disciplineName) == degree;
});
return listt;
}
关于lambda的使用,这只是使用功能对象的一种现代方式。您可以自由使用任何适合您的方法。在这种情况下,比较器(例如
std::less
或std::greater
)不合适,因为您使用自定义代码选择了必要的对象,并且未对它们进行排序。关于c++ - C++,带有vector <class>的copy_if,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55552746/