stable_sort внутри класса

У меня была проблема с типом при использовании std::stable_sort
Я продолжаю получать ошибку:

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

Я не могу понять, почему он появляется в качестве указателя... если кто-то может взглянуть, что будет высоко ценится.

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;
}

И 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 (в настоящее время в 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;
    }


};

Я уверен, что это что-то довольно простое, и я просто каким-то образом путаюсь с декларацией, но я застрял на ней некоторое время, так что второй взгляд был бы наиболее полезным.

1 ответ

Если вы планируете использовать функцию-член в качестве компаратора, переданного std::stable_sort() это должно быть static функция.

Другие вопросы по тегам