首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类中的stable_sort

类中的stable_sort
EN

Stack Overflow用户
提问于 2012-03-20 09:07:28
回答 1查看 668关注 0票数 1

我在使用std::stable_sort时遇到了一个类型问题

我一直收到错误:

代码语言:javascript
复制
argument of type 'bool (Memory::)(const Mem&, const Mem&)' does not match 'bool (Memory::*)(const Mem&, const Mem&)'

我不明白为什么它会以pointer...if的形式出现,有人可以看一看,这会很受欢迎。

Memory.cpp:

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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_ */

内存(当前在constants.hpp中)

代码语言:javascript
复制
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;
    }


};

我确信这是相当简单的东西,我只是在某种程度上搞乱了声明,但我已经被困了一段时间,所以第二双眼睛将是最有帮助的。

EN

回答 1

Stack Overflow用户

发布于 2012-03-20 10:12:52

如果您计划使用一个成员函数作为传递给std::stable_sort()的比较器,那么它需要是一个static函数。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9780150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档