Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用c+实现int、string、float和date对象数组+;模板?_C++_Templates - Fatal编程技术网

C++ 如何使用c+实现int、string、float和date对象数组+;模板?

C++ 如何使用c+实现int、string、float和date对象数组+;模板?,c++,templates,C++,Templates,编写一个程序,使用类模板创建和显示整数、浮点数、字符串和日期对象的数组(其中日期对象使用整数月、日、年属性对日期建模) 我能够显示整数、浮点数和字符串的数组,但是Date对象的数组有问题。我不知道如何从template类调用print date函数(在date类中) template< typename T > class Myarray { private: int size; T *myarray; public: // constructor wit

编写一个程序,使用类模板创建和显示整数、浮点数、字符串和日期对象的数组(其中日期对象使用整数月、日、年属性对日期建模)

我能够显示整数、浮点数和字符串的数组,但是Date对象的数组有问题。我不知道如何从template类调用print date函数(在date类中)

template< typename T > 
class Myarray {

private:
    int size;
    T *myarray;
public:
    // constructor with user pre-defined size
    Myarray(int s , T* array) {
        size = s;
        myarray = new T[size];

        for (size_t i = 0; i < size; ++i)
        {
            myarray[i] = array[i]; // Copy into object
        }

    }
    // calss array member function to set element of myarray 
    // with type T values
    void setArray(int elem, T val) {
        myarray[elem] = val;
    }

    // for loop to display all elements of an array
    void getArray() {
        for (int j = 0; j < size; j++) {
            // typeid will retriev a type for each value
            cout << setw(7) << j << setw(13) << myarray[j] <<endl;
        }
        cout << "-----------------------------" << endl;
    }

};

class Date {
private:
    int day;
    int month;
    int year;
public:
    Date() {
        day = month = year = 0;
    }
    Date(int day, int month, int year) {
        this->day = day;
        this->month = month;
        this->year = year;

    }

    void print_date(void) {
        cout << day << "/" << month << "/" << year << endl;
    }


};

int main()
{
    // instantiate int_array object of class array<int> with size 2
    int array1[] = { 1,2,3,4,5 };
    Myarray< int > int_array(5,array1);
    int_array.getArray();


    float array2[] = { 1.012, 2.324, 3.141, 4.221, 5.327 };
    Myarray<float> b(5, array2);
    b.getArray();


    std::string array3[] = { "Ch1","Ch2","Ch3","Ch4","Ch5" };
    Myarray<std::string> c(5, array3);
    c.getArray();


    Date array4[] = { Date(10, 18, 2019), Date(1, 01, 2019), Date(7, 04, 2019),
                    Date(12, 31, 2019), Date(12, 25, 2019) };
    Myarray<Date> d(5, array4);
    d.getArray();



    return 0;
}
模板
类Myarray{
私人:
整数大小;
T*myarray;
公众:
//具有用户预定义大小的构造函数
Myarray(int s,T*array){
尺寸=s;
myarray=新的T[大小];
对于(大小i=0;icout问题出在调用

Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'Date'
cout << setw(7) << j << setw(13) << myarray[j] <<endl;
friend std::ostream& operator<<(std::ostream& oss, const Date& d)
{
    oss << d.day << "/" << d.month << "/" << d.year << "\n";
    return oss;
}
Date d;
std::cout << d;