Monday, January 3, 2011

Benzoic Acid Quinine Ch2cl2 Naoh

C + +: problems with copying objects stream

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&lt char&gt &gt&)’:
/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&lt char,
std::char_traits&lt char&gt &gt::basic_ofstream(const std::basic_ofstream&lt char,
std::char_traits&lt char&gt &gt&)’:
/usr/include/c++/4.4/iosfwd:84:
note: synthesized method ‘std::basic_ios&lt char,
std::char_traits&lt char&gt &gt::basic_ios(const std::basic_ios&lt char,
std::char_traits&lt char&gt &gt&)’ first required here
/usr/include/c++/4.4/streambuf:
In copy constructor ‘std::basic_filebuf&lt char,
std::char_traits&lt char&gt &gt::basic_filebuf(const std::basic_filebuf&lt char,
std::char_traits&lt char&gt &gt&)’:
/usr/include/c++/4.4/streambuf:770:
error: ‘std::basic_streambuf&lt _CharT, _Traits>::
basic_streambuf(const std::basic_streambuf&lt _CharT, _Traits&gt&)
[with _CharT = char, _Traits = std::char_traits&lt char&gt]’ 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&lt char,
std::char_traits&lt char&gt &gt::basic_ofstream(const std::basic_ofstream&lt char,
std::char_traits&lt char&gt &gt&)’:
/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