嵌套定义
最近寫(xiě)python順手了,寫(xiě)c++程序各種小錯(cuò)誤。。。
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);} };這時(shí)的錯(cuò)誤是沒(méi)有定義Cmp, 之前認(rèn)為是現(xiàn)在前面聲明Cmp, 在Path后面再定義Cmp,就可以解決嵌套定義的問(wèn)題,其實(shí)還是考慮問(wèn)題太膚淺。
進(jìn)入fun函數(shù)定義,這時(shí)我們用到了mp,此時(shí)編譯器肯定要知道Cmp的對(duì)象有多大,因?yàn)橐xmp了。而Cmp的定義在下面,所以肯定會(huì)報(bào)錯(cuò)。
正確的應(yīng)該是:
#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); }
總結(jié)
- 上一篇: python严格的命名冲突
- 下一篇: 理解stl中的erase