读书人

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

发布时间: 2012-09-14 23:00:48 作者: rapoo

[求助]重载流运算符+模板,有编译错误
我写了几行程序,重载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; } 

读书人网 >C++

热点推荐