1

I can use the remove_if function with the test() function below. However, when I try to pass an object to remove_if, I get the error mentioned in this post's title. I personally think the error has something to do with variable/function scope.

my code:

#include "Student_info.h"
#include <string>
#include <vector>
#include "Grade_gen.h"
#include <algorithm>
#include <iterator>
#include <list>

using std::vector; using std::remove_if;

bool Grade_gen::has_passed(Student_info& student){

    if (student.grade() > 60)
      {
        return true;
      }
    return false;
}

bool test(Student_info& student)
{
    return true;
}

std::list<Student_info>::iterator Grade_gen::process_students(std::list<Student_info>& students)
{
    remove_if(students.begin(), students.end(), has_passed);

    // this is just to test the scope of the functions in the class, they work.
    std::list<Student_info>::iterator b = students.begin();
    has_passed(*b);
    return b;
}

#include "Student_info.h"
#include <string>
#include <vector>
#include <list>

class Grade_gen
{
public:
    std::vector<Student_info> students;
    bool has_passed(Student_info&);
    std::list<Student_info>::iterator process_students(std::list<Student_info>&);
private:

};
Bilal Siddiqui
  • 349
  • 3
  • 17
girvain
  • 19
  • 1
  • 5
  • 2
    The `has_passed` function is a member function. The `remove_if` predicate needs to be a non-member function (such as a class static function, or free standing function, or lambda). – Eljay Aug 10 '18 at 16:10
  • 1
    [Your rubber duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging) wants to know how `remove_if`, given only a list of `Student_info`, can infer the correct `Grade_gen` instance to use. – user4581301 Aug 10 '18 at 16:17
  • Note : When a member function makes no use of class members, consider making it a free function. More on that in [How Non-Member Functions Improve Encapsulation](https://stackoverflow.com/questions/1692084/how-non-member-functions-improve-encapsulation) – user4581301 Aug 10 '18 at 16:21
  • Apologies for the code not making any sense as I removed most of it as it was unrelated to the problem. I tried static but I guess I didn't do it right. I'll go try it again. Is static common for c++ or is non member functions more common, maybe even using friends? I'm coming from java so I naturally avoid static stuff. – girvain Aug 10 '18 at 16:28
  • 2
    To quote a wiser puppet than I, "You must unlearn what you have learned." Remember that Java is not C++ and they have little in common other than basic syntax. – user4581301 Aug 10 '18 at 16:38
  • Lol absolutely, i think I'd be better as a total newb. Thanks everyone that article put things in perspective and the static fixed the issue but I see why that's not what I want for this. – girvain Aug 10 '18 at 16:41
  • Apologies for brainfarting on the important part of your earlier comment. You will find `static` member and free functions used liberally all through C++, either targeting different needs. The `static` member function can interact with `private` data, for example and a non-`friend` free function cannot. I observe a tendency more toward `friend` free functions over `static` member functions, but I'm not sure why. Now that I'm wondering about it, I should probably go look it up. – user4581301 Aug 10 '18 at 18:11
  • While I'm on the topic of looking stuff up, the [C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md) contain a lot of well-reasoned suggestions. – user4581301 Aug 10 '18 at 18:12

1 Answers1

1

The problem is that you are iterating over the students container, which cannot see farther than the objects of type Student_info, but the method you want to call is defined in the Grade_gen class. To be able to use the object calling remove_if, I suggest to pass a lambda function which captures this (in that case, this is of type Grade_gen*).

remove_if(
    students.begin(),
    students.end(),
    [this](Student_info& si) { return this->has_passed(si); }
);

Example: https://ideone.com/TfMKDf

vdavid
  • 2,434
  • 1
  • 14
  • 15