当前位置:网站首页>Use of type dependent names must be prefixed with 'typename'
Use of type dependent names must be prefixed with 'typename'
2022-06-23 08:58:00 【Diga Altman】
Project scenario :
For testing purposes , Write a general iterator print template , as follows :
template<class Con>
void PrintContainer(const Con& x)
{
Con::const_iterator it = x.begin();
while (it != x.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
Problem description
I thought there was no problem , But after running, the following error is reported :
error C7510 The dependent name must be used with “typename” The prefix
Cause analysis :
First , Error message theory const_iterator This type is a dependent name , And the type dependent name should be typename The prefix . What is a subordinate name ?
Subordinate Name : template The name that appears in , Depend on if template Parameters in , Then it is a subordinate Name . And if it is nested in template parameters at the same time , Then it is a nested subordinate Name .
Take this question as an example ,const_iterator Is the name that appears in the template , At the same time, it depends on the template parameters Con, meanwhile const_iterator Nested in template parameters , therefore const_iterator Is a nested subordinate Name .
Non dependent names are names that do not depend on any template parameters .
Nested subordinate names can cause some problems :
template<typename Con>
void PrintContainer(const Con& c)
{
Con::const_iterator* x;
}
For the above code , If const_iterator It's a class , be x It's a point const_iterator The pointer to , But if const_iterator Is the name of a variable , It's a multiplication action .
So in C++ There is a parsing rule in : If the parser is template Encountered a nested subordinate name in , Then assume that the name is not a type , Unless by adding keywords typename To tell it's a type .
Solution :
We can increase typename keyword , tell C++ Parser ,const_iterator Is a type .
template<class Con>
void PrintContainer(const Con& x)
{
typename Con::const_iterator it = x.begin();
while (it != x.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
————————————————
Copyright notice : This paper is about CSDN Blogger 「Ssorrymaker」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/weixin_44614083/article/details/124612785
边栏推荐
- 瞄准海外宠物市场,「Grasphand 」做了一款独立于手机的智能追踪产品 | 早期项目
- Talk about the implementation principle of @autowired
- Leetcode topic analysis 3sum
- USB peripheral driver - debug
- Leetcode topic analysis group anagrams
- 简易学生管理
- Utilisation du cookie du module de demande de noeud
- Testing -- automated testing selenium (about API)
- 如何在 FlowUs、Notion 等笔记软件中使用矩阵分析法建立你的思维脚手架
- Summary of Arthas vmtool command
猜你喜欢

【活动报名】SOFAStack × CSDN 联合举办开源系列 Meetup ,6 月 24 日火热开启

636. Exclusive Time of Functions

636. Exclusive Time of Functions
![[learning resources] understand and love mathematics](/img/a3/e1b0915c48c85d17c48a4bee523424.png)
[learning resources] understand and love mathematics

Flink错误--Caused by: org.apache.calcite.sql.parser.SqlParseException: Encountered “time“
![[event registration] sofastack × CSDN jointly held the open source series meetup, which was launched on June 24](/img/e1/97c92290a2a5e68f05cdbd5bf525e8.png)
[event registration] sofastack × CSDN jointly held the open source series meetup, which was launched on June 24

JS mask important data of ID card and mobile phone number with * *

Geoserver添加mongoDB数据源

“教练,我想打篮球“ —— 给做系统的同学们准备的 AI 学习系列小册

Why use growth neural gas network (GNG)?
随机推荐
5-rotating Daisy - rotating canvas and timer
[QNX Hypervisor 2.2用户手册]5.6.1 Guest关机时静默设备
Flink错误--Caused by: org.apache.calcite.sql.parser.SqlParseException: Encountered “time“
125. Valid Palindrome
In depth interpretation of poca smart contract platform gear: the road to parallel architecture public chain
'coach, I want to play basketball!'—— AI Learning Series booklet for system students
4、 Database design
3-progressbar and secondary cropping
June 22, 2022: golang multiple choice question, what does the following golang code output? A:3; B:1; C:4; D: Compilation failed.
Le rapport d'analyse de l'industrie chinoise des bases de données a été publié en juin. Le vent intelligent se lève, les colonnes se régénèrent
在小程序中实现视频通话及互动直播的一种方法
Utilisation du cookie du module de demande de noeud
Flutter achieves the effect of selecting seats in the cinema!
【活动报名】SOFAStack × CSDN 联合举办开源系列 Meetup ,6 月 24 日火热开启
4- draw ellipse, use timer
"Coach, I want to play basketball" -- AI Learning Series booklet for students who are making systems
Deep analysis and Simulation of vector
2- use line segments to form graphics and coordinate conversion
438. Find All Anagrams in a String
Leetcode topic analysis h-index II