c++ - Converting an Array class to a template -


i trying convert array class template, , keep getting error "error c2955: 'array' : use of class template requires template"

this class after trying convert it.

// fig. 10.10: array.h // array class definition 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 greater 0" ) ),          ptr( new t[ size ] )     {        ( size_t = 0; < size; ++i )            ptr[ ] = 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 != ) // avoid self-assignment    {       // arrays of different sizes, deallocate original       // left-side array, allocate new left-side array       if ( size != right.size )       {          delete [] ptr; // release space          size = right.size; // resize object          ptr = new t[ size ]; // create space array copy       } // end inner if        ( size_t = 0; < size; ++i )          ptr[ ] = right.ptr[ ]; // copy array object    } // end outer if     return *this; // enables x = y = z, example } // end function operator=     bool operator==( const array &right ) const {    if ( size != right.size )       return false; // arrays of different number of elements     ( size_t = 0; < size; ++i )       if ( ptr[ ] != right.ptr[ ] )          return false; // array contents not equal     return true; // arrays equal } // end function operator==        // subscript operator const objects returns rvalue    t operator[]( int subscript ) const {    // check subscript out-of-range error    if ( subscript < 0 || subscript >= size )       throw out_of_range( "subscript out of range" );     return ptr[ subscript ]; // returns copy of element } // end function operator[]   private:    size_t size; // pointer-based array size    t *ptr; // pointer first element of pointer-based array }; // end class array template <typename t> istream &operator>>( istream &input, array &a ) {    ( size_t = 0; < a.size; ++i )       input >> a.ptr[ ];     return input; // enables cin >> x >> y; } // end function  template <typename t>    ostream &operator<<( ostream &output, const array &a ) {    // output private ptr-based array     ( size_t = 0; < a.size; ++i )    {       output << setw( 12 ) << a.ptr[ ];        if ( ( + 1 ) % 4 == 0 ) // 4 numbers per row of output          output << endl;    } // end     if ( a.size % 4 != 0 ) // end last line of output       output << endl;     return output; // enables cout << x << y; } // end function operator<< #endif 

the error coming isteam , ostream functions. use of class template error seems issue getting. help.

there little misuderstanding on templated class side, when create templated class need qualify methods should return templated class.

template<typename t> array {     const array<t>& operator=(const array<t>& right); } 

this because array doesn't exists without template type, if had used array , array, bur method returns array, how compiler should know wich array refering to?


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -