C + + to pass a Stream object as a function parameter

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.