尝试将基类转换为类模板

时间:2017-11-17 23:23:37

标签: c++ class templates

所以,我正在尝试将数组类转换为模板类,到目前为止,我已经将头文件和主文件代码合并为一个,从而产生了包含所有.cpp代码的头文件。虽然尝试将代码编译到主文件中会导致无数错误。现在,请在这里轻松告诉我,我仍然会在这里掌握C ++,并且有一些更高级的功能和能力超出我的想象,所以我事先道歉我并不是对所有功能都非常熟悉。代码如下。

#ifndef ARRAY_H
#define ARRAY_H

 #include <iostream>
 #include <iomanip>
#include <stdexcept> 

 using namespace std;

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

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

        ptr[i] = 0; // set pointer-based array element
} // end Array default constructor

template <typename GArray> Array::~Array()
{
    delete[] ptr; // release pointer-based array space
}  // destructor

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



  // overloaded assignment operator;
  // const return avoids: ( a1 = a2 ) = a3
template <typename GArray>  const Array &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 int[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=


template <typename GArray>  bool operator==(const Array &) const; // equality operator   

                                      // subscript operator for const objects returns rvalue
int operator[](int) const;
  private:
size_t size;
int *ptr; 
}; 


template <typename GArray> bool Array::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; 
       } 


 template <typename GArray> int Array::operator[](int subscript) const
{

if (subscript < 0 || subscript >= size)
    throw out_of_range("Subscript out of range");

return ptr[subscript];
 } 


  template <typename GArray> 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;
 } 


   template <typename GArray> 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;
} 

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

return output; // enables cout << x << y;
   } 



#endif

1 个答案:

答案 0 :(得分:0)

首先

如果将类定义为模板,则无需将成员函数声明告知为模板。只需在定义中创建模板,并在类模板参数中使用它们(如果它们是在类外定义的)。例如,

template <class T> class Test {
    void func1(T arg1);
};
template <class T> Test<T>::func1(T arg1) {
    // Your definition here
}

或在课堂内,

template <class T> class Test {
    void func1(T arg1) {
        //Your definition here
    }
};

因此您需要从代码中删除它们。

其次

你的班级有两个朋友职能。在我看来,它们也应该是模板功能,以便与您的班级一起正常工作。您可以在模板类中了解友情功能,了解here

了解如何将常规类转换为模板的一些好资源

  1. Converting regular class to template
  2. I learned from here
  3. Best place for c++ template tutorial
  4. 最后

    虽然我理解你想要什么,但是应该为类添加更多功能才能正常工作,经过一些修改后,这里是可编译的代码,可以作为了解你的错误的起点

    #ifndef ARRAY_H
    #define ARRAY_H
    
    #include <iostream>
    #include <iomanip>
    #include <stdexcept>
    
    using namespace std;
    
    template <typename GArray>
    class Array {
    private:
        size_t size;
        int *ptr;
    
    public:
        Array(int arraySize);
        ~Array();
    
        size_t getSize() const;
    
        const Array& operator=(const Array &right);
        bool operator==(const Array &) const;
        int operator[](int) const;
    
        friend std::ostream &operator<<(std::ostream &, const Array &);
        friend std::istream &operator>>(std::istream &, Array &);
    
    };
    
    template <typename GArray> bool Array<GArray>::operator==(const Array<GArray> &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;
    }
    
    
    template <typename GArray> int Array<GArray>::operator[](int subscript) const
    {
    
        if (subscript < 0 || subscript >= size)
            throw out_of_range("Subscript out of range");
    
        return ptr[subscript];
    }
    
    
    template <typename GArray> istream &operator>>(istream &input, Array<GArray> &a)
    {
        for (size_t i = 0; i < a.size; ++i)
            input >> a.ptr[i];
    
        return input; // enables cin >> x >> y;
    }
    
    
    template <typename GArray> ostream &operator<<(ostream &output, const Array<GArray> &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;
        }
    
        if (a.size % 4 != 0) // end last line of output
            output << endl;
    
        return output; // enables cout << x << y;
    }
    
    template<typename GArray>
    Array<GArray>::Array(int arraySize)
            : size(arraySize > 0 ? arraySize :
                   throw invalid_argument("Array size must be greater than 0")),
              ptr(new int[size]) {
        for (size_t i = 0; i < size; ++i)
            ptr[i] = 0; // set pointer-based array element
    }
    
    template<typename GArray>
    Array<GArray>::~Array() {
    
        //This should be like this
        for (int i = 0; i < size; ++i) {
            delete ptr[i];
        }
        // delete[] ptr; // release pointer-based array space
    }
    
    template <typename GArray>
    size_t Array<GArray>::getSize() const {
        return size; // number of elements in Array
    }
    
    template<typename GArray>
    const Array<GArray> &Array<GArray>::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 int[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
    }
    
    
    #endif
    
相关问题