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

标签 c++ templates

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

我能够显示整数、 float 和字符串的数组,但在处理 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;
}

获取错误信息:

Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'Date'

最佳答案

问题出在您的 getArray函数,正在调用 <<myarray这是类型 T :

cout << setw(7) << j << setw(13) << myarray[j] <<endl;

在这里,它期望 myarray[j]可以用 operator<< 调用这适用于 int , floatstd::string ,但是你的 Date类不提供 operator<<使用,它怎么知道应该如何输出?它不会知道调用 print_date .相反,您应该只提供 operator<<Date :

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;

同样,您的 MyArray 也可以使用它类。

关于c++ - 如何使用 C++ 模板实现 int、string、float 和 date 对象的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57154866/

相关文章:

c++ - 使用 std::string 作为缓冲区有缺点吗?

ruby-on-rails - 使用 rails 模板将行插入文件

c++ - 函数模板内的 decltype 和范围解析运算符

python - 从模板调用 Django View

C++ 需要将一个字符串与 200.000 个单词进行比较

c++ - 检查 operator() 是否存在

c++ - 如何跨 DLL 边界使用 Boost.Log?

c++ - 当函数返回由模板类型和另一个类型组成的类型时,模板参数推导

templates - Golang 从文件中嵌入 html

asp.net-mvc - Razor Engine无法在ASP.net 5 MVC 6中运行