如何从派生类的基本模板化接口使用友人cin / cout函数

问题描述 投票:-1回答:1

我已经定义了下一个具有cin和cout好友功能的模板化接口:

#ifndef IPRINTABLE_H
#define IPRINTABLE_H

#include <iostream>
using namespace std;

template <class T>
class IPrintable
{
public:
    virtual friend istream &operator>>(istream&, T&) = 0;
    virtual friend ostream &operator<<(ostream&, const T&) = 0;
};

#endif

我试图将这些功能植入这样的派生类中:

#ifndef DATE_H
#define DATE_H

#include "IPrintable.h"

class Date : public IPrintable<Date>
{
public:
    Date();
    Date(int, int, int);
    ~Date();

    void setDay(const int);
    void setMonth(const int);
    void setYear(const int);

    friend istream &operator>>(istream&, Date&);
    friend ostream &operator<<(ostream&, const Date&);

private:
    int day;
    int month;
    int year;
};

#endif

但是我得到的只是一堆错误:enter image description here我尝试在Internet上搜索答案,但没有发现任何帮助,这就是为什么我要您为解决此问题提供帮助。

我对在模板化接口中使用朋友函数的继承概念不熟悉,所以我在这里做错事的可能性很大。

先谢谢您

P.S。我该如何解决这个问题:

enter image description here

c++ templates cin cout friend
1个回答
2
投票

您不能使用friend函数virtual,因为它不是成员函数。您要执行的操作如下:

© www.soinside.com 2019 - 2024. All rights reserved.