user1177245
user1177245

Reputation: 119

List iterator find function error in c++

I have a compilation error: "no matching function for call to find(std::_List_iterator<Process>, std::_List_iterator<Process>, Process&)" in my c++ program.

The mfqueue class looks like:

MFQueue::MFQueue() {
        list<Process> queue;
        vector<int> ran;
        int time_quantum = 0;
        int run_for = 0;
}

MFQueue::MFQueue(int quantum) {
        list<Process> queue;
        vector<int> ran;
        int time_quantum = quantum;
        int run_for = 0;
}

"Process" is one of my class

bool MFQueue::contains(Process p) {
        list<Process>::iterator itr = find(queue.begin(), queue.end(), p);
        return (p == *itr);;
}

Does anyone know how to fix this problem? Thanks in advance!

Upvotes: 1

Views: 1665

Answers (3)

billz
billz

Reputation: 45410

You need to overload operator== for std::find algorithm, assume you want to compare id of Process say:

 bool operator==(const Process &lhs, const Process& rhs)
 {
   return lhs.id== rhs.id;
 }

now, you can get it working

#include <algorithm>
std::list<Process>::iterator itr = std::find(queue.begin(), queue.end(), p);

Upvotes: 1

Nik Bougalis
Nik Bougalis

Reputation: 10613

Why are you declaring list<Process> queue; in the constructors not using them?

Upvotes: 0

Robᵩ
Robᵩ

Reputation: 168626

Add #include <algorithm> to your CPP file.

Upvotes: 4

Related Questions