ofstream in a class file

Ok, so this is part of a larger project but here is a test file that I haven't been able to get running. I don't understand why the minute I put an ofstream object as an instance variable the program flips out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include "FileNotFoundException.h"

int main()
{
	using namespace std;
	
	try
	{
		std::cout << "This is a test.";
		throw FileNotFoundException("Testing...");
	}
	catch (FileNotFoundException & x)
	{
		//x.msg();
	}
	
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>
#include <fstream>

class FileNotFoundException
{
private:
	//std::string message;
	std::ofstream y;
	
public:
	FileNotFoundException(std::string x) : y("log.txt") { }
	//void msg() { std::cout << message; }
};


I've tried it both in visual studio and mingw. Here are the errors respectively.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
>c:\program files (x86)\microsoft visual studio 10.0\vc\include\fstream(1116): error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(176) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          This diagnostic occurred in the compiler generated function 'std::basic_ofstream<_Elem,_Traits>::basic_ofstream(const std::basic_ofstream<_Elem,_Traits> &)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]


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
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ios:43:0
,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:
40,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/iostream
:40,
                 from FileNotFoundException.h:5,
                 from test.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/bits/ios_base.h: In copy const
ructor 'std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)':
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/bits/ios_base.h:788:5: error:
'std::ios_base::ios_base(const std::ios_base&)' is private
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ios:45:0
,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:
40,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/iostream
:40,
                 from FileNotFoundException.h:5,
                 from test.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/bits/basic_ios.h:64:11: error:
 within this context
In file included from FileNotFoundException.h:6:0,
                 from test.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/fstream: In copy constructor '
std::basic_ofstream<char>::basic_ofstream(const std::basic_ofstream<char>&)':
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/fstream:588:11: note: synthesi
zed method 'std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)' first
required here
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ios:44:0
,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/ostream:
40,
                 from c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/iostream
:40,
                 from FileNotFoundException.h:5,
                 from test.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/streambuf: In copy constructor
 'std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)':
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/streambuf:800:7: error: 'std::
basic_streambuf<_CharT, _Traits>::basic_streambuf(const __streambuf_type&) [with
 _CharT = char; _Traits = std::char_traits<char>; std::basic_streambuf<_CharT, _
Traits>::__streambuf_type = std::basic_streambuf<char>]' is private
In file included from FileNotFoundException.h:6:0,
                 from test.cpp:1:
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/fstream:69:11: error: within t
his context
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/fstream: In copy constructor '
std::basic_ofstream<char>::basic_ofstream(const std::basic_ofstream<char>&)':
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/include/c++/fstream:588:11: note: synthesi
zed method 'std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<cha
r>&)' first required here
In file included from test.cpp:1:0:
FileNotFoundException.h: In copy constructor 'FileNotFoundException::FileNotFoun
dException(const FileNotFoundException&)':
FileNotFoundException.h:8:7: note: synthesized method 'std::basic_ofstream<char>
::basic_ofstream(const std::basic_ofstream<char>&)' first required here
test.cpp: In function 'int main()':
test.cpp:10:43: note: synthesized method 'FileNotFoundException::FileNotFoundExc
eption(const FileNotFoundException&)' first required here
The exception object that is thrown must be copyable.

FileNotFoundException is not copyable (it has a non-copyable member of type std::ofstream)
Is there any other way I can attain file output with an exception. I wanted to use this for a GUI program for logging and a convenient way of handling errors
Perhaps, have the exception keep information about the path of the file that was not found.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <stdexcept>
#include <string>

struct file_not_found : virtual std::runtime_error
{
    explicit file_not_found( const std::string& path_to_file )
       : std::runtime_error( "file '" + path_to_file + "' not found" ), path(path_to_file) {}

    const std::string path ;
};

int main()
{
    try
    {
        throw file_not_found( "my_logfile.txt" ) ;
    }
    catch( const std::exception& e )
    {
        std::cerr << "error: " << e.what() << '\n' ;
    }
}

Topic archived. No new replies allowed.