模板对象数组 [英] Array of templated objects

查看:147
本文介绍了模板对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的任务:

实现模板类,它将表示长方体,其中长度,宽度和高度可以是任何数据类型。
立方体数组也可以是模板函数的参数,所以重载所有需要的操作符。
(当比较立方体时,较大的是较大体积的)

Realize template class which will represent cuboid, where length, width and height can be any data type. Array of cuboids can also be argument of template function, so overload all needed operators. (When compare cuboids, larger is one with larger volume)

这是我做的:

#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);
}



我无法编译它(因为主程序的最后一行)。任何想法?

I can't compile it (because of last line in main program). Any idea?

推荐答案

如果你看看你的编译错误:

If you look at your compile error:

main.cpp:70:31: error: no matching function for call to 'cuboid<int>::cuboid(cuboid<int>*&, int&)'
     cuboid <int> arr(n, length);
                               ^

它告诉你,你没有合适的构造函数。你试图调用 cuboid< int>(cuboid< int> *,int),但你所有的都是 cuboid< int& *,int)。现在,我可以告诉你如何解决这个问题,但我会解决你的其他问题:你的设计。

It tells you that you don't have the appropriate constructor. You're trying to call cuboid<int>(cuboid<int>*, int), but all you have is cuboid<int>(int*, int). Now, I could tell you how to fix this specifically, but instead I will fix your other problem: your design.

您的问题是:


实现代表长方体的模板类,其中length,width和height可以是任何数据类型。

Realize template class which will represent cuboid, where length, width and height can be any data type.

您的解决方案的一部分是非常有意义的:

And part of your solution to that makes perfect sense:

template <class T>
class cuboid {
private:
    T length, width, height;
};

但为什么 T * 和长度?数组是什么?为什么要在构造函数中将该数组赋给 cuboid * ?该代码只编译,因为你从来没有实例化该构造函数。如果你想要一个 cuboid 的集合,正确的方法是使用一个容器类。例如:

But why does it also have a T* and a length? What is the array for? And why are you assigning that array to a cuboid* in your constructor? That code only compiles because you never instantiated that constructor. If you want a collection of cuboids, the right way to do that is to use a container class. Something like:

std::vector<cuboid<int>> cuboids;
for (int i = 0; i < length; ++i) {
    cin >> a >> b >> c;
    cuboids.push_back(cuboid(a, b, c));
}

写一个 a cuboid 的集合,即使您正确写入,也是违反单责任原则

Writing a class that is both a cuboid and a collection of them, even if you wrote it correctly, is a pretty clear violation of the single responsibility principle

这篇关于模板对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆