【发布时间】:2020-01-03 19:46:54
【问题描述】:
我已经定义了下一个具有 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
但我得到的只是一堆错误: 我尝试在互联网上搜索答案,但没有发现任何有用的信息,这就是为什么我请求您帮助解决这个问题。
我对在模板化界面中使用友元函数的继承概念不熟悉,所以我很可能在这里做错了。
提前谢谢你
附: 我该如何解决这个问题:
【问题讨论】:
-
一般来说,
friend's 不应该被使用。 -
错误应该发布为文本,而不是图像(从输出选项卡的“构建顺序”列表复制)。
-
@JesperJuhl 我正在编写这个程序作为我的任务的一部分,我必须创建一个 IO 接口
-
@mega5800 “我正在编写这个程序作为作业的一部分” - 这有什么关系?
标签: c++ templates cin cout friend