如何将struct Data Type传递给模板?

时间:2014-11-12 01:30:52

标签: c++

我有两个相互继承的模板类。我有一个结构日期,我想传递给其中一个类中的函数但是我在编译时遇到这个错误:

no operator found which takes a right-hand operand of type 'Date' (or there is no acceptable conversion)

错误在第95行中具体说明。

正如您在我的代码中看到的那样,我传递了不同的数据类型来测试函数是否有效。除了struct Date之外,它们都完美无缺。

我做错了什么?

我的代码:

#include <iostream>
#include <string>
using namespace std;

template <typename T> class B;     //forward declare

template <typename T>
class A
{
    T valuea;
public:
    A(){};
    T getValuea()
    {
        return valuea;
    }

    void setValuea(T x)
    {
        valuea = x;
    }
    A(const A &x)
    { 
        valuea = x.valuea; 
    }

    friend class B<T>;     //A<int> is a friend of B<int> 
};

template <typename T>
class B : A<T>
{
    T valueb;
public:
    using A<T>::setValuea;
    using A<T>::getValuea;
    B(){};
    T getValueb()
    {
        return valueb;
    }
    void setValueb(T x)
    {
        valueb = x;
    }
    B(const B &x)
    {
        valueb = x.valueb;
        this->valuea = x.valuea;
    }
};

struct Date
{
    int day;
    int month;
    int year;
};

int main()
{
    B<float> b;
    b.setValuea(1.34);
    b.setValueb(3.14);

    cout << "b.setValuea(1.34): " << b.getValuea() << endl
        << "b.setValueb(3.14): " << b.getValueb() << endl;

    B<int> a;
    a.setValuea(1);
    a.setValueb(3);

    cout << "a.setValuea(1): " << a.getValuea() << endl
        << "a.setValueb(3): " << a.getValueb() << endl;

    B<char> y;
    y.setValuea('a');
    y.setValueb('c');

    cout << "y.setValuea('a'): " << y.getValuea() << endl
        << "y.setValueb('c'): " << y.getValueb() << endl;

    B<string> u;
    u.setValuea("good");
    u.setValueb("morning");

    cout << "u.setValuea(good): " << u.getValuea() << endl
        << "u.setValueb(morning): " << u.getValueb() << endl;

    B<Date> p;
    p.setValuea({ 27, 10, 2014 });
    p.setValueb({ 2, 11, 2014 });

    cout << "p.setValuea({ 27, 10, 2014 }): " << p.getValuea() << endl
         << "p.setValueb({ 2, 11, 2014 }):  " << p.getValueb() << endl;


    system("Pause");
    return 0;
}

1 个答案:

答案 0 :(得分:1)

您需要为Date类提供operator<<,否则它不知道如何将其输出到流。您可以尝试以下方法:

struct Date
{
    int day;
    int month;
    int year;

    friend ostream& operator << (ostream& os, const Date& date)
    {
        return os << "Day: " << date.day << ", Month: " << date.month << ", Year: " << date.year << " ";
    }
};

实例:http://ideone.com/ehio6Q