简体   繁体   English

删除和find_if的stl错误

[英]stl error for remove and find_if

I'm using vector<clsStudent*> student for polymorphism, as I have derived local student and international student from base class student. 我将vector<clsStudent*>学生用于多态性,因为我是从基础班学生那里衍生出本地学生和国际学生的。 the problem now is I have errors for my functor. 现在的问题是我的函子有错误。 I have two methods for deleting. 我有两种删除方法。 Both are not working. 两者都不起作用。 How do I fix this? 我该如何解决? and which delete method is better? 哪种删除方法更好?

void deleteStudent(vector <clsStudent*>& student)
{
    cout << "Enter student name to delete";
    student.erase(remove(student.begin(), student.end(), nameToDelete), student.end());

    vector <clsStudent*>::iterator it = student.begin();
    while (it != student.end())
    {
         if (*it == nameToDelete)
         {
             it = student.erase(it);
         }
         else{
             ++it;
         }
     }
}

void searchStudent(const vector <clsStudent*>& s)
{
    string searchName;

    cout << "\nEnter student name to search for. Press [Q] to terminate." << endl;
    cin >> searchName;


    if (s.size() == 0)
        cout << "There is 0 student in the database.";

    while(searchName != "q")
    {
        vector<clsStudent*>::iterator it = std::find_if(s.begin(),
                                                s.end(),
                                                MatchName(searchName)); <---- ERROR here
}

int main()
{
    char choice;
    clsUniversityProgram objProgram[3];
    for (int x = 0; x < 3; x++)
    {
        objProgram[x].programInfo();
    }
    vector <clsStudent*> student;

    do
    {
    cout <<"\nPress [A] to add new student"
        << "\nPress [L] to list existing stundet"
        << "\nPress [M] to modify existing student"
        << "\nPress [O] to sort student data by name"
        << "\nPress [W] to write students data to file"
        << "\nPress [R] to read students data from file"
        << "\nPress [D] to delete a student"
        << "\nPress [X] to exit"
        << "\nEnter your choice: " << endl;
        choice = toupper(getch());

        switch(choice){
        case 'A':
            addStudents(student, objProgram);
            break;
        case 'L':
        {
            for(int x = 0; x < student.size(); x++)
            {
                student[x]->printStudentDetails();
                student[x]->print();
            }
            break;
        }
        case 'M':
            // modify
        case 'O':
            sortStudentbyName(student);
            break;
        case 'W':
            // write
            break;
        case 'R':
            // read
            break;
        case 'D':
            deleteStudent(student);
            break;
        case 'X':
            return 0;
        default:
            cerr << "Invalid input!" << endl;
            break;
        }
   } while (choice != 'L'
            && choice != 'M'
            && choice != 'O'
            && choice != 'W'
            && choice != 'R'
            && choice != 'D');
    return 0;
}
struct MatchName
    {
      MatchName(string& searchName) : s_(searchName) {}
      bool operator()(const clsStudent* student) const
      {
        return student->getName() == s_;
      }
     private:
      string s_;
    };

Errors 失误

error: conversion from '__gnu_cxx::__normal_iterator<clsStudent* const*, std::vector<clsStudent*> >' to non-scalar type 'std::vector<clsStudent*>::iterator {aka __gnu_cxx::__normal_iterator<clsStudent**, std::vector<clsStudent*> >}' requested|

The problem with find_if is that s is a const reference to the vector, but the iterator you're returning is non-const, so try changing to: find_if的问题在于s是对向量的const引用,但是您要返回的迭代器不是const,因此请尝试更改为:

vector<clsStudent*>::const_iterator it = std::find_if(...)

Or if you're using a C++11 compiler, then you can you use auto to infer the correct const/non-const iterator for you: 或者,如果您使用的是C ++ 11编译器,则可以使用auto来为您推断正确的const / non-const迭代器:

auto it=std::find_if(...)

You haven't posted the error for the call to remove , but I suspect it's because you really intended to write: 您尚未发布调用remove的错误,但是我怀疑这是因为您确实打算写:

remove_if(student.begin(), student.end(), MatchName(nameToDelete))

the error you report comes from your searchStudent method. 您报告的错误来自您的searchStudent方法。

your vector is const, so you can't use iterator, use const_iterator instead 您的向量是const,因此您不能使用迭代器,而应使用const_iterator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM