The following compile error (probably surrounded by a number of other lines for our specific code) returned by g + + compiler for a program written in C + + is a symptom of a very specific problem and sneaky:
/ usr / include / c + + / 4.4/bits/ios_base.h:
In copy constructor 'std:: basic_ios \u0026lt;char,
std:: char_traits \u0026lt;char>>:: basic_ios (const std:: basic_ios \u0026lt;char,
std::char_traits< char> >&)’:
/usr/include/c++/4.4/bits/ios_base.h:790: error:
‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/include/c++/4.4/iosfwd:47: error: within this context
/usr/include/c++/4.4/iosfwd:
In copy constructor ‘std::basic_ofstream< char,
std::char_traits< char> >::basic_ofstream(const std::basic_ofstream< char,
std::char_traits< char> >&)’:
/usr/include/c++/4.4/iosfwd:84:
note: synthesized method ‘std::basic_ios< char,
std::char_traits< char> >::basic_ios(const std::basic_ios< char,
std::char_traits< char> >&)’ first required here
/usr/include/c++/4.4/streambuf:
In copy constructor ‘std::basic_filebuf< char,
std::char_traits< char> >::basic_filebuf(const std::basic_filebuf< char,
std::char_traits< char> >&)’:
/usr/include/c++/4.4/streambuf:770:
error: ‘std::basic_streambuf< _CharT, _Traits>::
basic_streambuf(const std::basic_streambuf< _CharT, _Traits>&)
[with _CharT = char, _Traits = std::char_traits< char>]’ is private
/usr/include/c++/4.4/iosfwd:78: error: within this context
/usr/include/c++/4.4/iosfwd:
In copy constructor ‘std::basic_ofstream< char,
std::char_traits< char> >::basic_ofstream(const std::basic_ofstream< char,
std::char_traits< char> >&)’:
/usr/include/c++/4.4/iosfwd:84:
note: synthesized method 'std:: basic_filebuf \u0026lt;char,
std:: char_traits \u0026lt;char>>:: basic_filebuf (const std:: basic_filebuf \u0026lt;char,
std:: char_traits \u0026lt;char>> &)' first required here
All these words really can be summarized in this way: you're trying to copy an object that can not be copied. The object is, in this case, a stream standard library (in my case it was an object of class ofstream).
is important to remember that when you pass an object (or, more generally, a variable) to a function code corresponding invocation of the function will generate a copy of the past, even in this case thus makes an illegal operation and will tell the compiler, so not too clear, that we are wrong.
The solution is simple: just change the function and its invocation so that the past is to be the reference (ie pointer) to the object and not the object itself.
Wrong version: class myclass {
...
public:
void print (ofstream file) const;}
;
[...]
ofstream file;
File.Open (filename, ios:: out);
myclass obj ();
obj.print (file);
Corrected version: class myclass {
...
public:
void print (ofstream * file) const;}
;
[...]
ofstream file;
File.Open (filename, ios:: out);
class obj ();
obj. print (& file);
0 comments:
Post a Comment