I know that you cannot pass by value when dealing with ostream and other streams but the thing is I'm not passing by value and I am still getting an error pretty much saying the copy constructor is private,As I said this shouldn't be an issue as I am not passing by value yet am still getting an error
edit ** I have a question a follow up,I fixed the error
I had a ofstream object as private in another part of the code which isn't included in the above post but thats weird,why would the copy constructor be called when declaring an ofstream object private?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class Book
{
private:
string name;
string ISBN;
string author;
Genre genre;
string defaultISBN = "000-000-000-F";
string defaultName = "Default";
bool checkedOut
ofstream books
// more code
#include <iostream>
#include <fstream>
// return value of type std::ifstream
std::ifstream open_r( std::string fname ) { return std::ifstream(fname) ; }
// return value of type std::ofstream
std::ofstream open_wa( std::string fname ) { return std::ofstream( fname, std::ios::app ) ; }
// pass values of type std::ifstream, std::ofstream, return value of type std::ofstream
std::ofstream copy_text( std::ifstream fin, std::ofstream fout )
{
fout << fin.rdbuf() ;
return std::move(fout) ; // file streams can be moved
}
int main()
{
for( int i = 0 ; i < 3 ; ++i ) // append this file to copies.cpp (repeat three times)
if( copy_text( open_r(__FILE__), open_wa( "copies.cpp" ) ) << "/* end copy #" << i << " */\n\n" )
std::cout << i << ". contents of this file was appended to file copies.cpp\n" ;
}
> I had a ofstream object as private ...
> why would the copy constructor be called when declaring an ofstream object private?
The semantics of the implicitly declared copy constructor is member-wise copy initialisation of all non-static member objects (including private member objects).
The implicitly-declared or defaulted copy constructor for class T is defined as deleted if any of the following conditions are true:
T has non-static data members that cannot be copied (have deleted, inaccessible, or ambiguous copy constructors) http://en.cppreference.com/w/cpp/language/copy_constructor
There would be a futile attempt to invoke the deleted copy constructor if we try to copy an object of such a class.