【问题标题】:Trying to convert base class to a class template试图将基类转换为类模板
【发布时间】:2018-05-01 19:48:10
【问题描述】:

所以,我正在尝试将一个数组类转换为一个模板类,到目前为止,我已经将头文件和主文件代码合二为一,只生成了包含所有 .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

【问题讨论】:

  • 您的模板,它们没有任何用途。你想达到什么目的?
  • 拥有它以便函数可以处理所有变量类型,int、double、char 等。
  • 问题是什么?要我们调试代码吗?

标签: c++ class templates


【解决方案1】:

首先

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

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

终于

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

#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

【讨论】:

    猜你喜欢
    • 2017-01-13
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 2021-09-26
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    相关资源
    最近更新 更多