首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

[]重载流运算符+模板,有编译异常

2012-09-14 
[求助]重载流运算符+模板,有编译错误我写了几行程序,重载ostream来输出一个deque容器的内容。VC下编译不过。

[求助]重载流运算符+模板,有编译错误
我写了几行程序,重载ostream来输出一个deque容器的内容。VC下编译不过。 

C/C++ code
#include <iostream> #include <deque> using namespace std; template< typename T > ostream& operator<<( ostream, const deque< T >& collection ) {      for( auto it = collection.begin(); it != collection.end(); ++ it )      {          strm << *it;     }      return strm; } int _tmain(int argc, _TCHAR* argv[]) {      deque<int> di;      di.push_back(1);      di.push_back(2);      cout<<di<<endl;      return 0; } 

编译得到N行错误提示。到底错在哪里呢?

[解决办法]
C/C++ code
#include <iostream> #include <deque> using namespace std; template< typename T > ostream& operator<<( ostream &strm, const deque< T >& collection ) {      for( typename deque<T>::const_iterator it = collection.begin(); it != collection.end(); ++ it )      {          strm << *it;     }      return strm; } int main() {      deque<int> di;      di.push_back(1);      di.push_back(2);      cout<<di<<endl;  system("pause"); return 0; } 

热点排行