C ++程序不会编译 [英] C++ program will not complie
问题描述
以下简单程序无法编译:
The following simple program will not compile:
#include <map>
2
3 using namespace std;
4 ^M
5 template<class T, class A>
6 void ShowMap(const map<T, A>& v)
7 {
8 map<T, A>::const_iterator ci = v.begin();
9 return;
10
11 }
编译器产生以下错误信息;
g ++ program.cpp -o program
2 program.cpp:在函数'void ShowMap(const std :: map< t,>&)'中:
3 program.cpp:8:4:错误:在'std :: map< t,> :: const_iterator'之前需要'typename'因为'std :: map< t,>'是依赖范围
4 program.cpp:8:30:错误:预期';'在'ci'之前
5 make:*** [程序]错误1
~
有谁可以向我解释这个问题?
非常感谢
TCNM
The compiler produces the following error message;
g++ program.cpp -o Program
2 program.cpp: In function ‘void ShowMap(const std::map<t,>&)’:
3 program.cpp:8:4: error: need ‘typename’ before ‘std::map<t,>::const_iterator’ because ‘std::map<t,>’ is a dependent scope
4 program.cpp:8:30: error: expected ‘;’ before ‘ci’
5 make: *** [Program] Error 1
~
Can anyone explain the problem to me?
Thank you very much
TCNM
推荐答案
因为const_iterator是由std :: map类中的typedef创建的,依赖于尚未确定的类型T您的代码被解析为新的C ++ 11标准,您需要将其称为
Because of the way const_iterator is created by a typedef inside the std::map class dependent on the as yet undecided type T when your code is parsed the new C++11 standard says you need to refer to it as
typename map< T, A >::const_iterator ci = v.begin();
换句话说''通过在std :: map<中查找const_iterator来命名的类型; T,A>
模板很有趣,只要你不赶时间。
in other words ''the type that is named by looking up const_iterator in std::map< T, A >
templates are fun as long as you''re not in a hurry.
试试
...
auto ci = v.begin();
...
干杯
Andi
Cheers
Andi
这篇关于C ++程序不会编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!