【发布时间】:2015-11-14 17:51:53
【问题描述】:
我有抽象类(“父亲”类)和子类
我想在父亲身上写上操作符
这里是代码
#include <iostream>
using namespace std;
class father {
virtual friend ostream& operator<< (ostream & out, father &obj) = 0;
};
class son: public father {
friend ostream& operator<< (ostream & out, son &obj)
{
out << "bla bla";
return out;
}
};
void main()
{
son obj;
cout << obj;
}
我得到 3 个错误
错误 3 错误 C2852 : 'operator
错误 2 错误 C2575 : 'operator
错误 1 错误 C2254:'
请问我能做什么?
【问题讨论】:
-
全局函数不能是纯虚函数。
-
如果我想在标题上写“
-
你可以提供一个
protected纯虚函数std::ostream& put(std::ostream& os) const = 0;和一个全局函数friend ostream& operator<< (ostream & out, father &obj)来使用它。请注意,friend关系不会被继承。
标签: c++ class polymorphism operator-overloading