惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
Y
Y Combinator Blog
月光博客
月光博客
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
博客园 - 司徒正美
V
Visual Studio Blog
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
A
About on SuperTechFans
The Cloudflare Blog

博客园 - 可可

广东电信最新DNS更新了 分布式缓存失效处理 序列化 suse中计划任务工具cron的使用 转载《.NET框架设计之五------------异常设计(异常与返回值)》 《高级Bash脚本编程指南》在线阅读 转载《Linux Shell Bash 各种小技巧》 Linux多线程例子 转载《Adding a Second IP Address to an Existing Network Adapter on Linux》 SuSE下chkconfig的启动脚本参考例子"/etc/init.d/skeleton” - 可可 - 博客园 转载《Starting and Stoping Services: insserv》 一个公网IP情况下实现LVS VS/DR vmware中suse10不能上网解决方法 Using PHP with mod_fcgid Apache Configuration PHP在Apache中两种工作方式的区别(CGI模式、Apache 模块DLL) SSO 转贴《负载均衡技术介绍》 CDN
linux下c++程序编译错误--理解typename
可可 · 2009-04-10 · via 博客园 - 可可

在linux下编译模板程序出错,出错信息如下,而同样的代码在vs2003中编译通过,google了一下原来是使用vector<T>内部类型iterator时需要添加typename,加上typename后编译通过。

meta:/var/ftp/upload/test # g++ -o temp template.cpp
template.cpp: In function ‘std::string print(std::vector<Type, std::allocator<_CharT> >)’:
template.cpp:16: error: expected `;' before ‘it’
template.cpp:17: error: ‘it’ was not declared in this scope
template.cpp: In function ‘std::string print(std::vector<Type, std::allocator<_CharT> >) [with Type = int]’:
template.cpp:29:   instantiated from here
template.cpp:16: error: dependent-name ‘std::vector<Type,std::allocator<_CharT> >::iterator’ is parsed as a non-type, but instantiation yields a type
template.cpp:16: note: say ‘typename std::vector<Type,std::allocator<_CharT> >::iterator’ if a type is meant
代码如下:

//template.cpp

#include<iostream>
#include<vector>
#include<string>
#include<sstream>

using namespace std; 

template<class Type>
string print(vector<Type> vt)
{
 stringstream ss;
 for(vector<Type>::iterator it = vt.begin(); it < vt.end(); it++)
 {
  ss << *it << " " << endl;
 }
 return ss.str();
}

int main()
{
 vector<int> vi;
 vi.push_back(100);
 vi.push_back(123);
 string str = print(vi);
 cout << str;
 return 0;
}

typename两种用法:

1。用在模板声明中,如下两个声明是一样的:

template<class T> class Widget; // uses "class"
template<typename T> class Widget; // uses "typename"

2.用于标识 nested dependent type name(嵌套依赖类型名)