One, the compilation does not pass the code:
/******************************************************************************** File Name:./main.cpp* Author : zjw* Email: [Email protected]* Create time:2015 April 09 Thursday 17:36 02 Seconds **************************** ***************************************************/#include<iostream>#include<fstream>#include<string>using namespacestd;void Print (Ostream out, String StrText) { out<< StrText <<Endl;}intMainintargcChar**argv) {Print (cout,"cout"); Ofstream Fout ("./test.dat"); if(!Fout.is_open ()) {Print (cout,"Error"); } Else{Print (Fout,"Fout"); Fout.close (); } return 0;}
Error message:
InchfileIncluded from/usr/lib/GCC/x86_64-redhat-linux/4.4.6/.. /.. /.. /.. /include/c++/4.4.6/ios: the, from/usr/lib/GCC/x86_64-redhat-linux/4.4.6/.. /.. /.. /.. /include/c++/4.4.6/ostream: +, from/usr/lib/GCC/x86_64-redhat-linux/4.4.6/.. /.. /.. /.. /include/c++/4.4.6/iostream: +, from./main.CPP:8:/usr/lib/GCC/x86_64-redhat-linux/4.4.6/.. /.. /.. /.. /include/c++/4.4.6/bits/ios_base.h:in copy Constructor ' std::basic_ios<Char, std::char_traits<Char> >::basic_ios (const std::basic_ios<Char, std::char_traits<Char> >&) ':/usr/lib/GCC/x86_64-redhat-linux/4.4.6/.. /.. /.. /.. /include/c++/4.4.6/bits/ios_base.h:790: Error: ' Std::ios_base::ios_base (const std::ios_base&) ' is private/usr/lib/GCC/x86_64-redhat-linux/4.4.6/.. /.. /.. /.. /include/c++/4.4.6/IOSFWD: -: Error: In this context/usr/lib/GCC/x86_64-redhat-linux/4.4.6/.. /.. /.. /.. /include/c++/4.4.6/iosfwd:in copy Constructor ' std::basic_ostream<Char, std::char_traits<Char> >::basic_ostream (const std::basic_ostream<Char, std::char_traits<Char> >&) ':/usr/lib/GCC/x86_64-redhat-linux/4.4.6/.. /.. /.. /.. /include/c++/4.4.6/IOSFWD: About: Note: Here's the first time you need to generate the method ' std::basic_ios<Char, std::char_traits<Char> >::basic_ios (const std::basic_ios<Char, std::char_traits<Char> >&) './main.CPP: Infunction‘intMainint,Char**) ':./main.CPP: +: Note: Here's the first time you need to generate the method ' std::basic_ostream<Char, std::char_traits<Char> >::basic_ostream (const std::basic_ostream<Char, std::char_traits<Char> >&) './main.CPP: +: Error: Initializing ' void Print ' (Std::ostream, std::string) ' 's argument1
Second, the executable code:
/******************************************************************************** File Name:./main.cpp* Author : zjw* Email: [Email protected]* Create time:2015 April 09 Thursday 17:36 02 Seconds **************************** ***************************************************/#include<iostream>#include<fstream>#include<string>using namespacestd;voidPrint (Ostream & out,stringStrText) { out<< StrText <<Endl;}intMainintargcChar**argv) {Print (cout,"cout"); Ofstream Fout ("./test.dat"); if(!Fout.is_open ()) {Print (cout,"Error"); } Else{Print (Fout,"Fout"); Fout.close (); } return 0;}
Compile & Execute successfully.
Three, Summary:
What is not understood here is why passing a stream object is not possible, while passing a reference can. and the g++ compiler error message is not quite understand. You can only remember that you need to use the stream object to do parameters, using the reference.
C + + to pass a Stream object as a function parameter