expected a type, got 'cmp'-CSDN社区 (2025)

社区 C++ 语言

帖子详情

interim2012 2012-03-31 04:05:38

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.
不是可以用函数指针吗?为什么我的程序还会出错


#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;
}

...全文

607 12

打赏 收藏

分享

转发到动态

举报

AI 作业

写回复

用AI写文章

12 条回复

切换为时间正序

请发表友善的回复…

发表回复

www_adintr_com 2012-04-01

  • 打赏
  • 举报
回复

priority_queue <node, vector <node> , cmp> qn;
等价于
priority_queue <node, vector <node> , cmp> qn(cmp());
使用了默认参数。

interim2012 2012-03-31

  • 打赏
  • 举报
回复

谢谢
struct cmp{
bool operator() ( node a, node b ){
return a.priority < b.priority;
}
};
priority_queue<node, vector<node>, cmp> qn;

那这种又是什么用法呢?我只知道cmp类重载了()
传了cmp 这种类型进去,为什么就能实现优先级对列呢

www_adintr_com 2012-03-31

  • 打赏
  • 举报
回复

是的.

interim2012 2012-03-31

  • 打赏
  • 举报
回复

[Quote=引用 8 楼 的回复:]

使用函数对象的完整写法其实也应该是这样:

cmp cmp_obj(...);
priority_queue<node, vector<node>, cmp> qn(cmp_obj);

当 cmp 这个类有默认构造函数时可以有你那种简写的方法, 如果没有默认构造函数就需要像上面那样写了.
[/Quote]
cmp cmp_obj(...);
priority_queue<node, vector<node>, cmp> qn(cmp_obj);
我查了下Priority_queue的构造函数。
xplicit priority_queue ( const Compare& x = Compare(),
const Container& y = Container() );
这种用法:qn(cmp_obj);
是不是就是调用了这个构造函数,来初始化x

www_adintr_com 2012-03-31

  • 打赏
  • 举报
回复

使用函数对象的完整写法其实也应该是这样:

cmp cmp_obj(...);
priority_queue<node, vector<node>, cmp> qn(cmp_obj);

当 cmp 这个类有默认构造函数时可以有你那种简写的方法, 如果没有默认构造函数就需要像上面那样写了.

www_adintr_com 2012-03-31

  • 打赏
  • 举报
回复

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

Compare 是一个类型参数啊, 你只能传递一个类型给它, 不能把一个函数指针给它呀.
模版参数有两类, 一类是类型, 一类是整数. 函数指针算是整数的, 不能传给接收类型的.

interim2012 2012-03-31

  • 打赏
  • 举报
回复

上面的代码有错。重新贴下代码

priority_queue<node, vector<node>, cmp> qn;这种用法错了
下面这种对了。但不知道为什么要像下面这样子去用
priority_queue<node, vector<node>, bool(*)(node, node)> qn(cmp);


#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()
{


int len = 5;
int i;
//示例3
// priority_queue<node> qn;
// priority_queue<node, vector<node>, cmp> qn;
priority_queue<node, vector<node>, bool(*)(node, node)> qn(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;
}

interim2012 2012-03-31

  • 打赏
  • 举报
回复

[Quote=引用 2 楼 的回复:]

用函数指针的话你需要这样使用:

C/C++ code

priority_queue<node, vector<node>, bool(*)(node, node)> qn(cmp);
[/Quote]

谢谢。
但不明白用法为什么是这样?

Saingel 2012-03-31

  • 打赏
  • 举报
回复

[Quote=引用 3 楼 的回复:]

C/C++ code

struct cmp
{


bool operator ()(const node& n1, const node& n2) const
{
return n1.priority < n2.priority;
}
};
[/Quote]看错 - - 还以为是map 无视我把

Saingel 2012-03-31

  • 打赏
  • 举报
回复


struct cmp
{
bool operator ()(const node& n1, const node& n2) const
{
return n1.priority < n2.priority;
}
};

www_adintr_com 2012-03-31

  • 打赏
  • 举报
回复

用函数指针的话你需要这样使用:

priority_queue<node, vector<node>, bool(*)(node, node)> qn(cmp);

ouyh12345 2012-03-31

  • 打赏
  • 举报
回复

struct node
{
bool operator< (const node &n1, const node &n2) const
{
return n1.priority < n2.priority;
}
int priority;
int value;
};

expected a type, got 'cmp'-CSDN社区 (14) au3反编译源码

au3反编译源码myAut2Exe - The Open Source AutoIT Script Decompiler 2.9========================================================*New* full support for AutoIT v3.2.6++ :)... mmh here's what I merely missed in the 'public sources 3.1.0'This program is for studying the 'Compiled' AutoIt3 format.AutoHotKey was developed from AutoIT and so scripts are nearly the same.Drag the compiled *.exe or *.a3x into the AutoIT Script Decompiler textbox.To copy text or to enlarge the log window double click on it.Supported Obfuscators:'Jos van der Zande AutoIt3 Source Obfuscator v1.0.14 [June 16, 2007]' ,'Jos van der Zande AutoIt3 Source Obfuscator v1.0.15 [July 1, 2007]' ,'Jos van der Zande AutoIt3 Source Obfuscator v1.0.20 [Sept 8, 2007]' ,'Jos van der Zande AutoIt3 Source Obfuscator v1.0.22 [Oct 18, 2007]' ,'Jos van der Zande AutoIt3 Source Obfuscator v1.0.24 [Feb 15, 2008]' ,'EncodeIt 2.0' and'Chr() string encode'Tested with: AutoIT : v3. 3. 0.0 and AutoIT : v2.64. 0.0 and AutoHotKey: v1.0.48.5The options:==========='Force Old Script Type' Grey means auto detect and is the best in most cases. However if auto detection fails or is fooled through modification try to enable/disable this setting'Don't delete temp files (compressed script)' this will keep *.pak files you may try to unpack manually with'LZSS.exe' as well as *.tok DeTokeniser files, tidy backups and *.tbl (<-Used in van Zande obfucation). If enable it will keep AHK-Scripts as they are and doesn't remove the linebreaks at the beginning Default:OFF'Verbose LogOutput' When checked you get verbose information when decompiling(DeTokenise) new 3.2.6+ compiled Exe Default:OFF'Restore Includes' will separated/restore includes. requires ';

expected a type, got 'cmp'-CSDN社区 (15) Expected a type 的错误

在定义一个协议的时候,出现Expected a type 的错误?解决办法:#import <UIKit/UIKit.h>转载于:https://www.cnblogs.com/yaoyao0110/p/4793978.html

expected a type, got 'cmp'-CSDN社区 (16) 【CS Round #43 A】Expected Dice

【链接】https://csacademy.com/contest/round-43/task/expected-dice/【题意】大水题【题解】把36种可能的结果都存下来.然后把重复出现的次数最多的输出就好了。【错的次数】0【反思】在这了写反思【代码】#include<bits/stdc++.h>us...

expected a type, got 'cmp'-CSDN社区 (17) 2.python/pytorch编程debug

快速解决python中的bug,这里总有你需要的。

expected a type, got 'cmp'-CSDN社区 (18) [转]python模块全面

python模块http://www.cnblogs.com/wupeiqi/articles/4963027.html模块概念:用一砣代码实现了某个功能的代码集合。类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合。而对于一个复杂的功能来,可能需要多个函数才能完成(函数又可以在不同的.py文件中),n个 .py 文...

expected a type, got 'cmp'-CSDN社区 (2025)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Carlyn Walter

Last Updated:

Views: 6054

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.