template <class T, int x> class Array { public: void m(); // …… }; void f() { int a; Array<double, a> ar; }
时间: 2024-03-27 17:41:32 浏览: 18
这段代码存在一个问题:
在函数f中创建Array对象时,使用了一个非常量表达式a作为模板参数。而在模板实例化时,模板参数必须是一个常量表达式。因此,这段代码无法通过编译。
如果想要修正这个问题,需要将a改为一个常量表达式,比如:
```c++
void f() {
const int a = 10;
Array<double, a> ar;
}
```
或者使用一个常量表达式作为模板参数,比如:
```c++
template <class T> void f() {
const int a = 10;
Array<double, a> ar;
}
```
相关问题
1、理解下面的动态数组类模板,它由一系列位置连续、任意数量相同类型的元素组成,其元素个数可在程序运行时改变,并完成该类中没有完成的成员函数(不允许改变已有代码),并设计主函数,实现对该模板类的功能测试。 #include <iostream> using namespace std; #include <stdlib.h> //容错处理 enum ErrorType { invalidArraySize, memoryAllocatetionError, indexOutOfRang }; char *errorMsg[] = { "Invalid array size", "Memory allocation error", "Invalid index" }; template <class T> class Array { private: T* alist; int size; void Error(ErrorType error) const;//输出错误信息 public: Array(int sz=50);//构造函数 Array(const Array<T>& X);//拷贝构造函数 ~Array(void);//析构函数 Array<T>& operator=(const Array<T>& rhs);//重载赋值运算符 T& operator[](int i);//重载下标运算符 int GetSize(void) const;//获取数组大小 void Resize(int sz);//重新设置数组大小 }; template <class T> Array<T>::Array(int sz) { if(sz <= 0) Error(invalidArraySize); size = sz; alist = new T[size]; if(alist == 0) Error(memoryAllocatetionError); } template <class T> Array<T>::Array(const Array<T>& X) { int n = X.size; size = n; alist = new T[n]; if(alist == 0) Error(memoryAllocatetionError); T* srcptr = X.alist; T* destptr = alist; while(n--) *destptr++ = *srcptr++; } template<class T> Array<T>::~Array() { delete[] alist; } template <class T> Array<T>& Array<T>::operator=(const Array<T> &rhs) { int n = rhs.size; if(size != n) { delete[] alist; alist = new T[n]; if(alist == 0) Error(memoryAllocatetionError); size = n; } T* destptr = alist; T* srcptr = rhs.alist; while(n--) *destptr++ = *srcptr++; return *this; } template<class T> T& Array<T>::operator[](int n) { if(n < 0 || n > size-1) Error(indexOutOfRang); return alist[n]; }
以下是对 Array 类的成员函数的完成,并设计了主函数进行测试:
```
template<class T>
void Array<T>::Error(ErrorType error) const {
cout << "Error: " << errorMsg[error] << endl;
exit(1);
}
template<class T>
int Array<T>::GetSize() const {
return size;
}
template<class T>
void Array<T>::Resize(int sz) {
if (sz <= 0) {
Error(invalidArraySize);
}
T* newAlist = new T[sz];
if (newAlist == 0) {
Error(memoryAllocatetionError);
}
int n = (sz < size ? sz : size);
for (int i = 0; i < n; i++) {
newAlist[i] = alist[i];
}
delete[] alist;
alist = newAlist;
size = sz;
}
int main() {
Array<int> arr(5);
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
arr[4] = 5;
cout << "Array size: " << arr.GetSize() << endl;
for (int i = 0; i < arr.GetSize(); i++) {
cout << "arr[" << i << "] = " << arr[i] << endl;
}
arr.Resize(7);
arr[5] = 6;
arr[6] = 7;
cout << "Array size: " << arr.GetSize() << endl;
for (int i = 0; i < arr.GetSize(); i++) {
cout << "arr[" << i << "] = " << arr[i] << endl;
}
Array<string> strArr(3);
strArr[0] = "Hello";
strArr[1] = "World";
strArr[2] = "!";
cout << "Array size: " << strArr.GetSize() << endl;
for (int i = 0; i < strArr.GetSize(); i++) {
cout << "strArr[" << i << "] = " << strArr[i] << endl;
}
return 0;
}
```
该程序首先创建了一个大小为 5 的 `Array<int>` 对象,并对其进行了初始化。然后,调用 `GetSize()` 函数获取数组大小,并使用重载的下标运算符 `[]` 访问每个元素。接着,调用 `Resize()` 函数将数组大小更改为 7,并在新的两个位置上存储新的整数值。最后,创建了一个大小为 3 的 `Array<string>` 对象,并用字符串初始化它。同样,调用 `GetSize()` 函数并使用重载的下标运算符 `[]` 访问每个元素。
如下类声明有何错误#include<iostream> using namespace std; const int MAX_SIZE = 5; template<class T> class Array { T* pData; int nSize; public: Array(int n); //构造函数 ~Array(); //析构函数 //下标运算符重载 T& operator[](int idx){ return pData[idx]; } int Find(T& obj); //查找函数 void Sort(); //排序函数 }; template<class T> Array::Array(int n) { pData=new T[n]; nSize=n; } template<class T> int Array::Find(T& obj) { int i=0; for(;i<nSize;i++) { if(pData[i]==obj) return i; } return -1; } template<class T> void Array::Sort() { for(int i=0;i<nSize;i++) for(int j=0;j<nSize-i-1;j++) if(pDate[j]>pData[j+1]) { T t=pData[j]; pData[j]=pData[j+1]; pData[j+1]=t; } }
该类声明有以下错误:
1. 在类的模板函数实现中,应该在函数名后加上模板参数列表,例如:
```
template<class T>
Array<T>::Array(int n) {
pData = new T[n];
nSize = n;
}
```
2. 在类的模板函数实现中,应该在类名后加上模板参数列表,例如:
```
template<class T>
int Array<T>::Find(T& obj) {
int i = 0;
for (; i < nSize; i++) {
if (pData[i] == obj)
return i;
}
return -1;
}
```
3. 在类的模板函数实现中,应该在类名后加上模板参数列表,例如:
```
template<class T>
void Array<T>::Sort() {
for (int i = 0; i < nSize; i++)
for (int j = 0; j < nSize - i - 1; j++)
if (pData[j] > pData[j + 1]) {
T t = pData[j];
pData[j] = pData[j + 1];
pData[j + 1] = t;
}
}
```
4. 在类的构造函数和析构函数中,应该在类名后加上模板参数列表,例如:
```
template<class T>
Array<T>::Array(int n) {
pData = new T[n];
nSize = n;
}
template<class T>
Array<T>::~Array() {
delete[] pData;
}
```
5. 在类声明中,应该在模板参数列表后加上类名,例如:
```
template<class T>
class Array {
T* pData;
int nSize;
public:
Array(int n); //构造函数
~Array(); //析构函数
T& operator[](int idx); //下标运算符重载
int Find(T& obj); //查找函数
void Sort(); //排序函数
};
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)