一个类中的stable_sort

时间:2012-03-20 01:07:28

标签: c++ sorting struct 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 个答案:

答案 0 :(得分:3)

如果您计划使用成员函数作为传递给std::stable_sort()的比较器,则它必须是static函数。