2014-11-12 2 views
0

서로 상속하는 두 개의 템플릿 클래스가 있습니다. 그리고 나는 클래스 중 하나에서 함수에 전달하려는 구조체 날짜를하지만 컴파일 할 때이 오류를 얻을 :strcut 데이터 유형을 템플릿에 전달하는 방법은 무엇입니까?

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

당신은 당신의 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

+0

너무 감사합니다. 효과가 있습니다. – SaidAlbahri

관련 문제