将Array类转换为模板

时间:2017-04-07 11:50:32

标签: c++ templates

我正在尝试将我的数组类转换为模板,并且我一直收到错误"错误C2955:'数组' :使用类模板需要模板"

尝试转换后,这是我的课程。

// Fig. 10.10: Array.h
// Array class definition with overloaded operators.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <stdexcept> 
#ifndef ARRAY_H
#define ARRAY_H
using namespace std;

template<typename T>
class Array
{
   friend ostream &operator<<( ostream &, const Array & );
   friend istream &operator>>( istream &, Array & );

public:
   explicit Array(int arraySize = 3)// default constructor
       : size( arraySize > 0 ? arraySize : 
            throw invalid_argument( "Array size must be greater than 0" ) ),
         ptr( new T[ size ] )
    {
       for ( size_t i = 0; i < size; ++i )

          ptr[ i ] = 0; // set pointer-based array element
    }

   ~Array()// destructor
    {
       delete [] ptr; // release pointer-based array space
    }

   size_t getSize() const
    {
       return size; // number of elements in Array
    } // end function getSize

   const Array &operator=( const Array &right )
{
   if ( &right != this ) // avoid self-assignment
   {
      // for Arrays of different sizes, deallocate original
      // left-side Array, then allocate new left-side Array
      if ( size != right.size )
      {
         delete [] ptr; // release space
         size = right.size; // resize this object
         ptr = new T[ size ]; // create space for Array copy
      } // end inner if

      for ( size_t i = 0; i < size; ++i )
         ptr[ i ] = right.ptr[ i ]; // copy array into object
   } // end outer if

   return *this; // enables x = y = z, for example
} // end function operator= 
   bool operator==( const Array &right ) const
{
   if ( size != right.size )
      return false; // arrays of different number of elements

   for ( size_t i = 0; i < size; ++i )
      if ( ptr[ i ] != right.ptr[ i ] )
         return false; // Array contents are not equal

   return true; // Arrays are equal
} // end function operator==   

   // subscript operator for const objects returns rvalue
   T operator[]( int subscript ) const
{
   // check for subscript out-of-range error
   if ( subscript < 0 || subscript >= size )
      throw out_of_range( "Subscript out of range" );

   return ptr[ subscript ]; // returns copy of this element
} // end function operator[]


private:
   size_t size; // pointer-based array size
   T *ptr; // pointer to first element of pointer-based array
}; // end class Array
template <typename T>
istream &operator>>( istream &input, Array &a )
{
   for ( size_t i = 0; i < a.size; ++i )
      input >> a.ptr[ i ];

   return input; // enables cin >> x >> y;
} // end function 
template <typename T>
   ostream &operator<<( ostream &output, const Array &a )
{
   // output private ptr-based array 
   for ( size_t i = 0; i < a.size; ++i )
   {
      output << setw( 12 ) << a.ptr[ i ];

      if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output
         output << endl;
   } // end for

   if ( a.size % 4 != 0 ) // end last line of output
      output << endl;

   return output; // enables cout << x << y;
} // end function operator<<
#endif

错误来自isteam和ostream函数。 使用类模板错误似乎是我得到的唯一问题。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

对于一个模板化类来自你身边有一点误解,当你创建一个模板化类时,你需要完全限定应该返回模板化类的方法。

template<typename T> Array {
    const Array<T>& operator=(const Array<T>& right);
}

这是因为如果没有使用模板类型,Array就不存在,如果你使用过数组和数组,那么你的方法会返回一个数组,编译器应该如何知道数组是你所引用的?