输出流重载的问题
#include<iostream>#include"s.h"#include<cstring>#include<ostream>usingnamespacestd;vo...
#include <iostream>
#include "s.h"
#include<cstring>
#include <ostream>
using namespace std;
void Sales_item::print()
{
std::cout<<this->isbn<<endl<<this->price<<endl<<this->writer<<endl;
}
bool Sales_item::same_isbn(const Sales_item &rhs)
{
return this->isbn==rhs.isbn;
}
Sales_item::ostream &operator <<(ostream &out,Sales_item &s)/*报错: 'ostream' in 'class Sales_item' does not name a type*/
{
out<<this->isbn<<" "<<this->price<<" "<<this->writer;
return out;
} 展开
#include "s.h"
#include<cstring>
#include <ostream>
using namespace std;
void Sales_item::print()
{
std::cout<<this->isbn<<endl<<this->price<<endl<<this->writer<<endl;
}
bool Sales_item::same_isbn(const Sales_item &rhs)
{
return this->isbn==rhs.isbn;
}
Sales_item::ostream &operator <<(ostream &out,Sales_item &s)/*报错: 'ostream' in 'class Sales_item' does not name a type*/
{
out<<this->isbn<<" "<<this->price<<" "<<this->writer;
return out;
} 展开
2个回答
展开全部
重载流插入和流读取运算符
a.这两个运算符本身可以处理char*字符串和指针在内的各种内部数据类型;
b.这两个操作符重载函数只能够定义为类的友元函数
[cpp] view plaincopy
friend ostream & operator <<(ostream & output,const PhoneNumber &);
friend istream & operator >>(istream & input,PhoneNumber &);
操作符重载函数包含两个参数,一个是对istream/ostream的引用,另一个是对用户自定义 类型的引用,当编译器遇到语句 cin>>phone时,编译器将生成函数调用operator>>(cin,phone) 同样,编译器督导cout<<phone时,编译器将生成函数调用operator<<(cout,phone) 同样,编译器督导cout<<phone时,编译器将生成函数调用operator<<(cout,phone)
非原创,参考文库里的一篇文章,未验证,仅供参考
a.这两个运算符本身可以处理char*字符串和指针在内的各种内部数据类型;
b.这两个操作符重载函数只能够定义为类的友元函数
[cpp] view plaincopy
friend ostream & operator <<(ostream & output,const PhoneNumber &);
friend istream & operator >>(istream & input,PhoneNumber &);
操作符重载函数包含两个参数,一个是对istream/ostream的引用,另一个是对用户自定义 类型的引用,当编译器遇到语句 cin>>phone时,编译器将生成函数调用operator>>(cin,phone) 同样,编译器督导cout<<phone时,编译器将生成函数调用operator<<(cout,phone) 同样,编译器督导cout<<phone时,编译器将生成函数调用operator<<(cout,phone)
非原创,参考文库里的一篇文章,未验证,仅供参考
参考资料: http://wenku.baidu.com/view/77e8d29151e79b89680226a3.html
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询