passing on ofstream by reference error

Hi guys

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

thanks


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  bool addBook(ofstream &storeBooks)
    {

        string name;
        string author;
        string ISBN;
        Genre genre;
        char choice;

        cout << "Enter the name of the Book" << endl;
        getline(cin,name);

        cout << "Enter name of the Author" << endl;
        getline(cin,author);

        cout << "Enter ISBN" << endl;
        getline(cin,ISBN);

        // implement genre ask user to choose genre
        genre = BIO;

        for(int i = 0; i < books.size(); i++)
        {
            if(name == books.at(i).getName() && author == books.at(i).getAuthor())
            {
                cout << "the library already has a book by that Author"
                     << "is it another book? type y for yes n for no" << endl;
                cin >> choice;
                cin.get();
                if(choice == 'n')
                {
                    cout << "first false" << endl;
                    return false;
                }
            }
            if(ISBN == books.at(i).getISBN())
            {
                cout << "sec false" << endl;
                return false;
            }
        }

        cout << "adding book" << endl;
        books.push_back(Book(name,ISBN,author,BIO));
        storeBooks<< name << endl;
        storeBooks << ISBN << endl;
        storeBooks << author << endl;
        storeBooks << genre << endl;
    }

int main()
{


    ofstream storeBooks("storeBooks.txt",ofstream::app);

    cout << c << endl;

    Library M;
    M.addPatron();
    M.addBook(storeBooks);
    M.printBooks();
}
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
> you cannot pass by value when dealing with ostream and other streams

File streams are moveable (C++11 and later); they can be passed (and returned) by value.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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" ;
}

http://coliru.stacked-crooked.com/a/b83ad1e56de7c435



> 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.
Topic archived. No new replies allowed.