C ++, copy_if с вектором <class> - PullRequest
0 голосов
/ 06 апреля 2019

Это часть кода, с которой у меня проблемы

#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, но что я делаю не так?Это просто перегрузка оператора?Также я хочу спросить, правильно ли здесь используется лямбда.Должен ли я просто заменить его компаратором (я использую его один раз)?

...