這裏是我的任務:陣列模板對象
實現模板類這將表示長方體,其中長度,寬度和高度可以是任何數據類型。 長方體的數組也可以是模板函數的參數,所以需要重載所有需要的運算符。 (當比較長方體,更大的是一個體積較大)
這裏是我做過什麼:
#include <iostream>
using namespace std;
template <class T>
class cuboid{
private:
int length_of_array;
T length, width, height;
T * arr;
public:
cuboid();
cuboid(T *, int);
cuboid(T, T, T);
~cuboid();
cuboid(const cuboid &);
T volume();
};
template <class T>
cuboid<T>::cuboid(){
}
template <class T>
cuboid<T>::cuboid (T *n, int len){
length_of_array = len;
arr = new cuboid <T> [length_of_array];
for(int i = 0; i < length_of_array; i++){
arr[i] = n[i];
}
}
template <class T>
cuboid<T>::cuboid(T o, T s, T v){
length = o;
width = s;
height = v;
}
template <class T>
cuboid<T>::~cuboid(){
delete [] arr;
arr = 0;
}
template <class T>
T cuboid<T>::volume(){
return length * width * height;
}
template <class T>
cuboid<T>::cuboid(const cuboid & source){
length_of_array = source.length_of_array;
arr = new cuboid <T> [length_of_array];
for(int i = 0; i < length_of_array; i++){
arr[i] = source.arr[i];
}
}
int main(){
int a, b, c, length;
cuboid <int> *n;
cout << "How many cuboids array contains? " << endl;
cin >> length;
n = new cuboid <int> [length];
for(int i = 0; i < length; i++){
cin >> a >> b >> c;
n[i] = cuboid <int> (a,b,c);
}
cuboid <int> arr(n, length);
}
我不能編譯(因爲在主程序最後一行)。任何想法?
一個非常明顯違反你應該表現出確切的錯誤信息以及代碼 –
最後一行需要一個構造函數,其中包含'cuboid *'和'int'。你沒有任何這樣的構造函數。 –