gpt4 book ai didi

c++ - 类内的 stable_sort

转载 作者:行者123 更新时间:2023-11-30 03:02:57 26 4
gpt4 key购买 nike

我在使用 std::stable_sort
时一直遇到类型问题我不断收到错误消息:

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

我不明白为什么它显示为指针...如果有人能看一下,将不胜感激。

内存.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;
}

和内存.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_ */

内存(当前在 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;
}


};

我确信这是相当简单的事情,我只是以某种方式弄乱了声明,但我已经坚持了一段时间,所以第二双眼睛会很有帮助。

最佳答案

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

关于c++ - 类内的 stable_sort,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9780150/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com