第一个模板类(templet<class elemType> class xxxxx)

   小弟是刚刚加入c++大队的,对c++中的基本知识只是稍微懂一点。今天在看《c++ primer》中的模板类时,就将其中的程序在vs 2008中写下,新建了Array.h和Array.cpp两个文件。

主函数:

ExpandedBlockStart.gif Main.cpp
 1  #include  " stdafx.h "
 2  #include  < iostream >
 3  #include  " Array.h "
 4 
 5  using   namespace  std;
 6  int  main( int  argc,  char   * argv[])
 7  {
 8      Array < char >  array;
 9      cout << array.size() << endl;
10 
11       char  z;
12      cin  >> z;
13       return   0 ;
14  }
15 
16 

 

 头文件:

ExpandedBlockStart.gif Array.h
 1  #pragma  once
 2 
 3 
 4  template < class  elemType >
 5  class  Array
 6  {
 7  public :
 8      
 9       explicit  Array( int  sz = DefaultArraySize);
10      Array(Array < elemType >   * array , int  array_size);
11      Array( const  Array  & rhs);
12 
13       ~ Array( void );
14 
15       // 判断是否相等操作
16       bool   operator == ( const  Array & ) const ;
17       bool   operator != ( const  Array & const ;
18 
19       // 赋值操作符
20      Array &   operator   = ( const  Array & );
21       int  size()  const { return  _size;};
22 
23      elemType &   operator []( int  index);
24       void  sort();
25      elemType min();
26      elemType max() ;
27       // ************************************
28       //  Method:    find
29       //  FullName:  IntArray::find
30       //  Access:    public 
31       //  Returns:   int
32       //  Qualifier:查找数组中是否存在特定的值。
33       //  Parameter: int value
34       // ************************************
35       int  find( const  elemType  & value);
36  protected :
37       void  Init( int  sz,elemType  * array);
38 
39  private :
40       static   const   int  DefaultArraySize = 12 ;
41       int  _size;
42      elemType  * ia;
43       bool  sorted;
44  };
45 

 

Array头文件的实现:

ExpandedBlockStart.gif Array.cpp
  1  #include  " StdAfx.h "
  2  #include  " Array.h "
  3  #include  < cassert >
  4 
  5  template < class  elemType >  Array < elemType > ::Array( int  sz)
  6  {
  7       Array < elemType > ::Init(sz, 0 );
  8  }
  9 
 10 
 11  template < class  elemType >   Array < elemType > ::Array(Array < elemType >   * array , int  array_size)
 12  {
 13      Array < elemType > ::Init(array_size,array);
 14  }
 15 
 16 
 17  template < class  elemType >  Array < elemType > ::Array( const  Array < elemType >   & rhs)
 18  {
 19      Array < elemType > ::Init(rhs.size(),rhs.ia);
 20  }
 21 
 22  template < class  elemType >  Array < elemType > :: ~ Array( void )
 23  {
 24      delete []ia;
 25      std::cout << " 析构函数作用中。。。。。 " << std::endl;
 26  }
 27  template < class  elemType >
 28  void  Array < elemType > ::Init( int  sz,elemType  * array)
 29  {
 30      _size  = sz;
 31      ia = new  elemType[_size];
 32      sorted = false ;
 33 
 34       for  ( int  ix = 0 ;ix < _size; ++ ix)
 35      {
 36           if  ( ! array)
 37              ia[ix] = 0 ;
 38           else
 39              ia[ix] = array[ix];
 40      }
 41  }
 42  template < class  elemType >
 43  elemType &  Array < elemType > :: operator  []( int  index)
 44  {
 45      assert(index > 0 || index < size());
 46           return  ia[index];
 47  }
 48 
 49 
 50  // ************************************
 51  //  Method:    find
 52  //  FullName:  IntArray::find
 53  //  Access:    public 
 54  //  Returns:   int
 55  //  Qualifier:查找数组中是否存在特定的值。
 56  //  Parameter: int value
 57  // ************************************
 58  template < class  elemType >
 59  int  Array < elemType > ::find( const  elemType  & value)
 60  {
 61       for  ( int  ix = 0 ;ix < _size; ++ ix)
 62      {
 63           if (ia[ix] == value)
 64               return  ix + 1 ;
 65      }
 66  }
 67  template < class  elemType >
 68  elemType Array < elemType > ::max()
 69  {
 70       return  ia[ 0 ];
 71  }
 72 
 73  template < class  elemType >
 74  elemType Array < elemType > ::min()
 75  {
 76       return  ia[size() - 1 ];
 77  }
 78 
 79  template < class  elemType >
 80  void  Array < elemType > ::sort()
 81  {
 82      elemType temp;
 83       // 如果已经排序,返回。
 84       if (sorted)
 85           return ;
 86 
 87       for  ( int  i = 0 ;i < size() - 1 ; ++ i)
 88      {
 89           for ( int  j = 1 ;j < size() - i; ++ j)
 90          {
 91               if (ia[i] < ia[j])
 92              {
 93                  temp = ia[j];
 94                  ia[j] = ia[i];
 95                  ia[i] = temp;
 96              }
 97          }
 98      }
 99 
100 
101       // 设置为以排序。
102      sorted  = true ;
103  }
104 
105 

 

但是在编译的时候vs 2008提示“错误 1 error LNK2019: 无法解析的外部符号 "public: __thiscall Array<char>::~Array<char>(void)" (??1?$Array@D@@QAE@XZ),该符号在函数 _main 中被引用 Esonion.obj Esonion" 。

 

    到网上找了好久,才找到原来是vs 2008不支持模板定义和实现分离。将Array.cpp的内容拷贝到Array.h中,在编译就可通过了。我在c-free中试了,也是不可以。看来这两个编译器都不支持模板定义和实现分离。

    特意记下来,c++学习路上的一个脚印。

 

转载于:https://www.cnblogs.com/mht_two/archive/2010/10/10/1847338.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值