当前位置:首页
开发技术指南» 文章正文
    引言:

 ·c#里写sql问题    »显示摘要«
    摘要: 我想写查找数据库某一字段为空的句子,该字段类型为日期型 string str="select * from 表名 where 字段名= ? "; 问号里应怎样写 ......
    摘要: rt,谢谢 ......


在模板类中定义友元函数的问题

源代码:  
   
  #include   <fstream>  
  using   namespace   std;  
   
  ifstream   fin("abc.txt");  
   
  template   <class   T>  
  class   abc{  
      T   data;  
  public:  
      friend   void   operator>>(   ifstream   &,   abc<T>   &   );  
  };  
   
  template   <class   T>  
  void   operator>>(   ifstream   &fin,   abc<T>   &t   )  
  {  
      fin   >>   t.data;  
  }  
   
  int   main()  
  {  
      abc<int>   t;  
      fin   >>   t;  
      return   0;  
  }  
   
  用g++编译,但编译警告operator>>后要有<T>,连接错误:找不到operator>>函数。  
 

NO.1   作者: healer_kx

这样写很好啊,不会编译出错吧?·

NO.2   作者: qfeng_zhao

template   <class   T>  
  class   abc{  
      T   data;  
  public:  
      friend   void   operator>>   <T>   (   ifstream   &,   abc<T>   &   );     //声明必须指定模板参数  
  };

NO.3   作者: healer_kx

g++?   我kao,   那不知道了。   可能你加个const什么的就可以了吧?    
  Linux对语言要求很严格的。

NO.4   作者: foochow

去掉名称空间试试..  
  #include   <fstream.h>

NO.5   作者: qfeng_zhao

template   <class   Type>  
  class   QueueItem   {  
  friend   class   foobar<Type>;  
  friend   void   foo<Type>(   QueueItem<Type>   );  
  friend   void   Queue<Type>::bar();  
  //   ...  
  };  
   
  foo()的友元声明的语法看起来或许令人吃惊  
  friend   void   foo<Type>(   QueueItem<Type>   );  
  函数名后面紧跟着显式的模板实参表foo<type>   这种语法可用来指定该友元声明所引  
  用的函数模板foo()的实例如果省略了显式的模板实参如下所示  
  friend   void   foo(   QueueItem<Type>   );  
  则友元声明会被解释为引用了一个非模板函数且该函数的参数类型是类模板

NO.6   作者: foochow

http://www.vckbase.com/document/viewdoc/?id=501  
   
  friend   void   operator>><>(ifstream&,   abc<T>&);你试试这个

NO.7   作者: yhbttfile

#include   <fstream>  
  using   namespace   std;  
   
  ifstream   fin("abc.txt");  
   
  template   <class   T>  
  class   abc{  
      T   data;  
  public:  
      template   <class   U>  
      friend   ifstream&   operator>>(   ifstream   &,   abc<U>   &   );  
  };  
   
  template   <class   U>  
  ifstream&   operator>>(   ifstream   &fin,   abc<U>   &t   )  
  {  
      fin   >>   t.data;  
      return   fin;  
  }  
   
  int   main()  
  {  
      abc<int>   t;  
      fin   >>   t;  
      return   0;  
  }  
 


 ·关于文件操作    »显示摘要«
    摘要: 我需打开一个文本文件,然后查找是否存在指定字符串,如果没有就添加 该有哪个函数查找. ......
» 本期热门文章:

©2000-2007 All Rights Reserved. 最佳浏览:1024X768 MSIE