简体   繁体   English

编译C ++代码时出现编译错误

[英]compilation error while compiling C++ code

I am trying to compile a C++ source file but I hit the below error when I try to compile it. 我正在尝试编译C ++源文件,但是当我尝试编译它时,我遇到了以下错误。

Error : 错误:

error: 'template class std::map' used without template parameters 错误:'template class std :: map'在没有模板参数的情况下使用

I get the error in the line which has mp.find(someString) . 我得到了mp.find(someString)行中的错误。

map<string, int *> mp;
sub = "xyz";
if(mp.find(sub) != map::end) {  
   doSomething();
}

I get the error when the if statement is executed. 执行if语句时出现错误。

How can I resolve this issue? 我该如何解决这个问题?

use mp.end() instead of map::end 使用mp.end()而不是map::end

update : 更新:

if(mp.find(sub) != map::end) { 

to

if(mp.find(sub) != mp.end()) {
//                 ^^^^^^^^

see std::map::end reference 请参阅std :: map :: end reference

You need to use mp.end : 您需要使用mp.end

 if(mp.find(sub) != mp.end() ) {

this reference for find shows a more extensive example. 该参考find显示了一个更广泛的例子。

You should update your code to : 您应该将代码更新为:

if(mp.find(sub) != mp.end())
//                 ^^^^^^^^

The end() method returns an iterator referring to the past-the-end element in the map container. end()方法返回一个迭代器,引用map容器中的past-the-end元素。

http://www.cplusplus.com/reference/map/map/end/ http://www.cplusplus.com/reference/map/map/end/

Maybe you were trying to use std::end who is a C++11 feature : 也许你试图使用std::end谁是C++11特性:

if(mp.find(sub) != std::end(mp))
//                 ^^^^^^^^^^^^

In this case, std::end(mp) has exactly the same behaviour as mp.end() . 在这种情况下, std::end(mp)mp.end()具有完全相同的行为。

Take a look at the documentation : http://www.cplusplus.com/reference/iterator/end/ 请查看文档: http//www.cplusplus.com/reference/iterator/end/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM