最近写python顺手了,写c++程序各种小错误。。。
tt.h
#include <set>
using namespace std;
struct Cmp;
class Path {
public:
typedef multiset<Path*, Cmp> Mp;
void fun(Mp& mp, int ss) {
Path *p = new Path;
p->s = 0;
mp.insert(p);
}
int s;
};
struct Cmp {
bool operator () (const Path* a, const Path*b) const {
return (a->s > b->s);
}
};
这时的错误是没有定义Cmp, 之前认为是现在前面声明Cmp, 在Path后面再定义Cmp,就可以解决嵌套定义的问题,其实还是考虑问题太肤浅。
进入fun函数定义,这时我们用到了mp,此时编译器肯定要知道Cmp的对象有多大,因为要定义mp了。而Cmp的定义在下面,所以肯定会报错。
正确的应该是:
#include <set>
using namespace std;
struct Cmp;
class Path {
public:
typedef multiset<Path*, Cmp> Mp;
void fun(Mp& mp, int ss);
int s;
};
struct Cmp {
bool operator () (const Path* a, const Path*b) const {//如果依次输出Mp里的值,是从大到小排序的
return (a->s > b->s);
}
};
void Path::fun(Mp& mp, int ss){
Path *p = new Path;
p->s = 0;
mp.insert(p);
}