c++ - 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
任何建议都会很棒,谢谢安迪
解决方案
推荐阅读
- python - 如何在python中将时间“now-5m”转换为日期时间对象
- javascript - 应用程序关闭时是否删除了侦听器?
- javascript - 赛普拉斯:网站端的一些请求得到空响应
- python - 在 vs 代码中的 python 测试文件中看不到调试/运行测试
- c# - Unity3d VS Code 配置问题
- android - Android:(谷歌地图)通过 Intent 禁用画中画模式
- c# - 如何将linq c#中日期之间的期间相加到特定期间
- macos - 如何在 macOS 中创建虚拟界面?
- go - 使用带有特定本地端口的 net.DialTimeout
- ruby-on-rails - Rspec 失败,因为救援错误时参数太少