Overloaded flow operator

Overloaded flow operator

Today, I suddenly use the overload of ">" and "< to summarize a blog

1. I / O stream (istream,ostream)

The following codes should be included first:

#include <iostream>
using namespace std;

My code:

//Input and output stream overloads' > 'and' < 'should be implemented by friend function using friend keyword
//Declare friend function ware.h
    friend ostream &operator<<(ostream &output,ware & Ware);//I overload in ware class
    friend istream &operator>>(istream &output,ware & Ware);
    //Implement friend function.cpp (usually in the corresponding source file)
    istream &operator>>(istream &input,ware & Ware){
    cout<<"name:";
    input>>Ware.name;
    cout<<"price:";
    input>>Ware.price;
    cout<<"salevolume:";
    input>>Ware.saleVolume;
    return input;
    }
    ostream &operator<<(ostream &output,ware & Ware){
        output<<Ware.name<<" "<<Ware.price<<" "<<Ware.saleVolume<<" ";
        return output;  
    };

2. File stream (fsstream, OFSTREAM, ifstream)

The following codes should be included first:

#include <iostream>
#include <fstream> 
using namespace std;

Note: fsstream, ofstream and ifstream are different. Fsstream variables cannot use ofstream overloaded '<', and vice versa. Fsstream variables cannot use ifstream overloaded '>', and vice versa
For example, the following code cannot be compiled. The error information is as follows:

//Error message:
error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class ware' (or there is no acceptable conversio

Header file ware.h

#pragma once
#include <iostream>
#include <fstream>  
#include <string>
using namespace std;
class ware{
public:
    string name;
    int saleVolume;
    int price;
    ware(){};
    ware(string name,int saleVolume,int price);
    friend ostream &operator<<(ostream &output,ware & Ware);
//  friend istream &operator>>(istream &output,ware & Ware);
    friend fstream &operator<<(fstream &output,ware & Ware); 
    friend fstream &operator>>(fstream &input,ware & Ware);
//  friend ifstream &operator>>(ifstream &input,ware & Ware); 
    friend ofstream &operator<<(ofstream &output,ware & Ware);
    ~ware();
};

ware.cpp:

//Overload in source file > >
........
fstream &operator<<(fstream &output,ware & Ware){//output to a file
    output<<Ware.name<<" "<<Ware.price<<" "<<Ware.saleVolume<<endl;
    return output;  
}
ofstream &operator<<(ofstream &output,ware & Ware){//output to a file
    output<<Ware.name<<" "<<Ware.price<<" "<<Ware.saleVolume<<endl;
    return output;  
}
fstream &operator>>(fstream &input,ware & Ware){//Import from file to object
    input>>Ware.name;
    input>>Ware.price;
    input>>Ware.saleVolume;
    return input;
}
ifstream &operator>>(ifstream &input,ware & Ware){//Import from file to object
    input>>Ware.name;
    input>>Ware.price;
    input>>Ware.saleVolume;
    return input;
}
istream &operator>>(istream &input,ware & Ware){
    cout<<"istream &operator>>(istream &input,ware & Ware)"<<endl;
    cout<<"name:";
    input>>Ware.name;
    cout<<endl<<"price:";
    input>>Ware.price;
    cout<<endl<<"salevolume:";
    input>>Ware.saleVolume;
    return input;
}
ostream &operator<<(ostream &output,ware & Ware){
    output<<Ware.name<<" "<<Ware.price<<" "<<Ware.saleVolume<<" ";
    return output;  
};
........

main.cpp

#include <iostream>
#include <string>
#include "ware.h"
#include <fstream>
using namespace std;
int main(){
    ifstream f("1.txt");//Only "'>' overload of fsstream, i fstream overload declaration is annotated
    if (!f){
        cout<<"fail to open file."<<endl;
        exit(0);
    }
    ware ware1;
    f>>ware1;
    ware ware2;
    cout<<ware1<<endl;
    f.close();
    return 0;
}

The above file ware.cpp is the overloaded function I wrote. By comparison, we can see that cout is equivalent to ofstream,fstream(ios::in);cin is equivalent to istream,fstream(ios::in)

Conclusion:

Did you notice that 13 lines of ware.h were commented?
//This is the driving force for me to write this article
When you remove the comments and run the above code, you will find that the compilation links are normal. The output is:

istream &operator>>(istream &input,ware & Ware)
name:
price:
salevolume:warename 10 10
Press any key to continue

Screenshot as evidence:

”f> The input and output stream overloaded function is called. The breakpoint debugging is very clear
I don't understand the reason (I guess it may be related to the inheritance relationship), I hope some Daniel can answer it
ps:
I use vc6.0;

My ability is limited, welcome to point out the mistake

reference material:

A concise course of c + + by Peter Chang. Electronic Industry Press

Tags: iOS

Posted on Tue, 05 May 2020 04:48:52 -0700 by nade93