kreynolds
kreynolds

Reputation: 446

stable_sort within a class

I have been having an issue with type when using std::stable_sort
I keep getting the error:

argument of type 'bool (Memory::)(const Mem&, const Mem&)' does not match 'bool (Memory::*)(const Mem&, const Mem&)'

I cannot figure out why it is showing up as a pointer...if someone could take a look that would be much appreciated.

Memory.cpp:

#include <vector>
#include <algorithm>
#include <iostream>
#include <set>

#include "Memory.h"
#include "constants.hpp"

Memory::Memory(int _type) {
    fit_type = _type;
    blocks = 1;
    mem[0].address = 0;
    mem[0].size = 2048;
    mem[0].item_id = EMPTY;
}

void Memory::sort_mem_list() {
    // Sort by address
    std::stable_sort(mem.begin(), mem.end(), Memory::compareByAddress );
}

bool Memory::compareByAddress(const Mem &a, const Mem &b) {
    return a.address < b.address;
}

And Memory.hpp

#ifndef MEMORY_H_
#define MEMORY_H_

#include <vector>
#include "process.h"
#include "constants.hpp"

class Memory {
public:
    Memory(int);
    int add_item(Process pr);
    void remove_item();
    void sort_mem_list();
    void set_fit_type(int);
    void memory_dump();

private:
    bool compareBestFit(const Mem & a, const Mem & b);
    bool compareWorstFit(const Mem & a, const Mem & b);
    bool compareByAddress(const Mem & a, const Mem & b);
    bool compareByProcess(const Mem & a, const Mem & b);

    int fit_type;
    int memory_size;
    int blocks;
    std::vector<Mem> mem;
};

#endif /* MEMORY_H_ */

Mem ( currently in constants.hpp )

struct Mem {
    int address;
    int size;
    int item_id;

    Mem() {
        address = 0;
        size = 0;
        item_id = EMPTY;
    }
    Mem(int a, int b, int c) {
        address = a;
        size = b;
        item_id = c;
    }


};

I am sure it is something fairly simple and I am just messing up the declaration somehow, but I have been stuck on it for a while so a second set of eyes would be most helpful.

Upvotes: 1

Views: 832

Answers (1)

Bukes
Bukes

Reputation: 3708

If you're planning on using a member function as the comparator passed to std::stable_sort() it needs to be a static function.

Upvotes: 3

Related Questions