【发布时间】:2012-04-04 12:39:33
【问题描述】:
我在使用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;
}
还有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;
}
};
我确信这是相当简单的事情,我只是以某种方式搞砸了声明,但我已经坚持了一段时间,所以第二双眼睛会很有帮助。
【问题讨论】:
-
通过在前面粘贴
&来传递指向函数的指针 -
非常感谢!我知道这很愚蠢
-
编译器在喊:你正在传递我
bool (Memory::)(const Mem&, const Mem&)但我要求一个指针 bool (Memory::*)(const Mem&, const Mem& ) -- 那是编译器在跟你说话 ;-) -
如果您发现自己花费了太多时间而收效甚微或毫无结果,请尝试散散步或以其他方式分散自己的注意力。 我的两分钱
-
看起来你需要一个“橡皮鸭”来解释你的问题,这样你就可以自己看看问题出在哪里
标签: c++ sorting struct stable-sort