首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

error: expected a type, got 'cmp'解决方法

2012-04-23 
error:expected a type, got cmpmain.cpp:31: error: type/value mismatch at argument 3 in template p

error: expected a type, got 'cmp'
main.cpp:31: error: type/value mismatch at argument 3 in template parameter list for 'template<class _Tp, class _Sequence, class _Compare> class std::priority_queue'

main.cpp:31: error: expected a type, got 'cmp'

template < class T, class Container = vector<T>,
  class Compare = less<typename Container::value_type> > class priority_queue;

Compare: Comparison class: A class such that the expression comp(a,b), where comp is an object of this class and a and b are elements of the container, returns true if a is to be placed earlier than b in a strict weak ordering operation. This can either be a class implementing a function call operator or a pointer to a function.
不是可以用函数指针吗?为什么我的程序还会出错

C/C++ code
#include<iostream> #include<functional> #include<queue> using namespace std; struct node {//     friend bool operator< (node n1, node n2)//     {//         return n1.priority < n2.priority;//     }     int priority;     int value; };//第一种是正确的//struct cmp{//    bool operator() ( node a, node b ){//        return a.priority < b.priority;//    }//};//这种出错了bool  cmp ( node a, node b ){    return a.priority < b.priority;} int main() {         priority_queue<node, vector<node>, cmp> qn; ///error:  expected a type, got 'cmp'     node b[len];     b[0].priority = 6; b[0].value = 1;     b[1].priority = 9; b[1].value = 5;     b[2].priority = 2; b[2].value = 3;     b[3].priority = 8; b[3].value = 2;     b[4].priority = 1; b[4].value = 4;     for(i = 0; i < len; i++)         qn.push(b[i]);     cout<<"优先级"<<'\t'<<"值"<<endl;     for(i = 0; i < len; i++)     {         cout<<qn.top().priority<<'\t'<<qn.top().value<<endl;         qn.pop();     }     return 0; }


[解决办法]
priority_queue <node, vector <node> , cmp> qn; 
等价于
priority_queue <node, vector <node> , cmp> qn(cmp()); 
使用了默认参数。

热点排行
Bad Request.