首页 > 解决方案 > C++ ArrayStack 中的构造函数问题

问题描述

我正在构建 StackArray。我已经有一个用构造函数实现的“Stack.h”。我想知道我会在我的“StackArray.h”文件中做什么来使用 Stack.h 文件。我正在考虑使用继承,但它给了我一个错误。

我的代码如下:

数组.h

#include <iostream>
#include <assert.h>

using namespace std;
#ifndef _ARRAY_H
#define _ARRAY_H

template<class T>
class Array{
  private:
    T *a;
    int length;
  public:
    // constructor
    Array (int len){
      length = len;
      a = new T[length];
      for (int i = 0; i < len; i++){
        a[i]=0;
      }
    }
    // destructor
    ~Array()
      {delete[] a;}
    // operator overload
    T& operator [](int i){
      assert (i>=0 && i < length);
      return a[i];
    }

    //get the length of the array
    int arraylength(){
      return length;
    }
};
#endif

ArrayStack.h

#ifndef _ARRAYSTACK_H_
#define _ARRAYSTACK_H_

#include "Array.h"

using namespace std;

template<class T>
class ArrayStack
{
  protected:
    Array<T> a;
    int n;
  public:
    ArrayStack(int len);
    virtual ~ArrayStack();
};


template<class T>
ArrayStack<T>::ArrayStack(int len){
 // I don't know what to do here to implemented constructor from another class.
}

#endif

任何建议都会很棒,谢谢安迪

标签: c++arrays

解决方案


推荐阅读