I am trying to get rid of various GCC warnings when strict checking is
turned on. One of these warnings is where it expects the programmer to
initialise every private data member in the initialisation list.
The trouble is, when I add:
m_mystream(std::of_stream())
I get a compilation error about not being able to copy private ios
data. It looks like it is trying to invoke a copy ctor to do the init
and maybe that is not on.
What can I do about this please? How come the default init is ok but
trying to do it explicitly in the code does not work?
Regards,
Andrew Marlow
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
marlow.andrew400 (9)
|
3/22/2011 2:30:58 PM |
|
On Mar 22, 4:30 pm, Andrew <marlow.and...@gmail.com> wrote:
> I am trying to get rid of various GCC warnings when strict checking
> is turned on. One of these warnings is where it expects the
> programmer to initialise every private data member in the
> initialisation list.
Which is stupid, imo. If the object is not mentioned in the
initializer list, it will be default constructed.
> The trouble is, when I add:
> m_mystream(std::of_stream())
>
> I get a compilation error about not being able to copy private ios
> data. It looks like it is trying to invoke a copy ctor to do the
> init and maybe that is not on.
Assuming 'std::of_stream' is std::ofstream and m_mystream is also an
std::ofstream, this creates a temporary using the default ctor and
initializes m_mystream using the copy-ctor. Standard streams are not
copyable.
> What can I do about this please? How come the default init is ok but
> trying to do it explicitly in the code does not work?
class a
{
public:
a()
: m_mystream()
{
}
private:
std::ofstream m_mystream;
};
--
Jonathan Mcdougall
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Jonathan
|
3/22/2011 8:34:14 PM
|
|
On 3/22/2011 1:30 PM, Andrew wrote:
>
> I am trying to get rid of various GCC warnings when strict checking is
> turned on. One of these warnings is where it expects the programmer to
> initialise every private data member in the initialisation list.
>
> The trouble is, when I add:
> m_mystream(std::of_stream())
>
> I get a compilation error about not being able to copy private ios
> data. It looks like it is trying to invoke a copy ctor to do the init
> and maybe that is not on.
>
> What can I do about this please? How come the default init is ok but
> trying to do it explicitly in the code does not work?
Streams are not copyable.
Hard to say what you should do to fix it. Don't know what you're trying
to do.
--
http://crazycpp.wordpress.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Noah
|
3/22/2011 8:36:58 PM
|
|
On Mar 22, 1:30 pm, Andrew <marlow.and...@gmail.com> wrote:
> I am trying to get rid of various GCC warnings when strict checking is
> turned on. One of these warnings is where it expects the programmer to
> initialise every private data member in the initialisation list.
>
> The trouble is, when I add:
> m_mystream(std::of_stream())
>
> I get a compilation error about not being able to copy private ios
> data. It looks like it is trying to invoke a copy ctor to do the init
> and maybe that is not on.
>
> What can I do about this please? How come the default init is ok but
> trying to do it explicitly in the code does not work?
>
Not having seen your code, I can only guess, but...
std::ofstream is not copyable. Try using a *REFERENCE* to a
std::ostream
in your class.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
red
|
3/22/2011 8:38:05 PM
|
|
Andrew wrote:
> I am trying to get rid of various GCC warnings when strict checking
> is turned on. One of these warnings is where it expects the
> programmer to initialise every private data member in the
> initialisation list.
>
> The trouble is, when I add:
> m_mystream(std::of_stream())
>
> I get a compilation error about not being able to copy private ios
> data. It looks like it is trying to invoke a copy ctor to do the
> init and maybe that is not on.
>
> What can I do about this please? How come the default init is ok but
> trying to do it explicitly in the code does not work?
>
> Regards,
>
> Andrew Marlow
Streams are default constructible, but not copyable (C++03). You can
use the default constructor in the initialisation list, just use
m_mystream()
Bo Persson
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Bo
|
3/22/2011 8:38:26 PM
|
|
On Mar 22, 8:30 pm, Andrew <marlow.and...@gmail.com> wrote:
> I am trying to get rid of various GCC warnings when strict checking is
> turned on. One of these warnings is where it expects the programmer to
> initialise every private data member in the initialisation list.
>
> The trouble is, when I add:
> m_mystream(std::of_stream())
I'm assuming of_stream is simply a typo for ofstream. I'm also
assuming that m_mystream is a class member of type std::ofstream, and
this code is in initialiser list for a constructor
This code is not legal because std::ofstream doesn't have a copy
constructor. If you want to default construct the m_mystream member,
then you need to default construct that, not a temporary which you
then copy into m_mystream. The syntax for doing that is:
my_class::my_class( /* any arguments... */ )
: m_mystream()
{ /* other code... */ }
But really, I don't see any point in doing this at all. It's needless
verbiage that does nothing but hide the important parts. Just do:
my_class:my_class( /* any arguments... */ )
{ /* other code... */ }
It's cleaner. And if a code lint, or a compiler configured to use a
high warning level, doesn't like it, think about changing the lint or
compiler settings.
Richard Smith
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Richard
|
3/22/2011 8:42:57 PM
|
|
On Mar 23, 10:17 am, Richard Smith <rich...@ex-parrot.com> wrote:
> On Mar 23, 2:38 am, red floyd <redfl...@gmail.com> wrote:
>
> > On Mar 22, 1:30 pm, Andrew <marlow.and...@gmail.com> wrote:
>
> >> [..] The trouble is, when I add:
> >> m_mystream(std::of_stream())
>
> >> I get a compilation error about not being able to copy private ios
> >> data. [...]
>
> > Not having seen your code, I can only guess, but...
>
> > std::ofstream is not copyable. Try using a *REFERENCE* to a
> > std::ostream in your class. [...]
>
> No! Don't do this!
>
> First of all, if the reference is a non-const reference, it won't
> compile; and if it's a const reference, there isn't much useful you
> can do with it. (Certainly all of the functions that actually do I/O
> are non-const.) Second, and far more seriously, the ofstream will be
> destroyed when the constructor exits. [See 12.2 [class.temporary],
> paragraph 5; although the text has been reformatted in C++0x as a list
> of bullet points, I can see no substantive, relevant changes.] So
> what you actually end up using is a reference to a destroyed stream
> object. Depending on your implementation, you may find that it is
> still vaguely usable, though that would surprise me a little; it's
> definitely not legal, though.
Missed the typo on of_stream and the std:: part. I guess I need new
glasses. I figured he had an ofstream he wanted as part of his class.
Call me old and blind.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
red
|
3/24/2011 12:54:38 AM
|
|
|
6 Replies
333 Views
(page loaded in 0.057 seconds)
Similiar Articles: comp.lang.c++.moderatedquestion: what to do about warning initialising std::ofstream private data member 6 221 (3/22/2011 2:30:58 PM) I am trying to get rid of various GCC warnings when ... Const constructor - comp.lang.c++.moderated... mutable and a pointer to const data. Do ... mutator_func(); } private: std ... exanation of what I am trying to do early in this post. Anyway, my question ... comp.lang.c++.moderated | Computer Groupquestion: what to do about warning initialising std::ofstream private data member 6 221 (3/22/2011 2:30:58 PM) I am trying to get rid of various GCC warnings when ... C++ / fstream as a class member - AllExperts Questions & AnswersQuestion Would you give an example code how ... the address-of operator (&) before initialising the (pointer) iStream member. ... A revised example (using std::ofstream as I am ... 7/26/2012 6:22:45 PM
|