Why Doesn't C++ Accept This OFSTREAM VECTOR?

Discussion in 'C++' started by Marina, Jul 28, 2004.

  1. Marina

    Marina Guest

    I get an "access violation" when I use someting like this:

    @@@@@@@@@@@@@@
    string tempo;
    const char *output;
    vector <ofstream> outs(3);
    ....
    ....
    open_output=(const char *)tempo.c_str();
    ofstream out0(open_output, ios::eek:ut | ios::eek:ut);
    outs[0]=out0;

    @@@@@@@@@@@@@@
    This access violation comes only when
    the line

    outs[0]=out0;

    is included.
    Please help. I do need a vector ot "ofstream" type.

    Thank you!

    Marina
     
    Marina, Jul 28, 2004
    #1
    1. Advertising

  2. Marina wrote:
    > I get an "access violation" when I use someting like this:
    >
    > @@@@@@@@@@@@@@
    > string tempo;
    > const char *output;
    > vector <ofstream> outs(3);


    This shouldn't compile. 'ofstream' doesn't satisfy the requirement for
    elements of 'vector': it is not "copy-constructible".

    > ...
    > ...
    > open_output=(const char *)tempo.c_str();


    There is no need to cast it. 'c_str()' already returns const char*.

    > ofstream out0(open_output, ios::eek:ut | ios::eek:ut);

    ^^^^^^^^^^^^^^^^^^^^^^ Huh?
    > outs[0]=out0;


    There is no copy semantics for ofstream. This simply shouldn't compile.

    Why can't you simply say

    outs[0].open(open_output);

    >
    > @@@@@@@@@@@@@@
    > This access violation comes only when
    > the line
    >
    > outs[0]=out0;
    >
    > is included.
    > Please help. I do need a vector ot "ofstream" type.


    You can't have it. Create a vector of pointers to ofstream.

    V
     
    Victor Bazarov, Jul 28, 2004
    #2
    1. Advertising

  3. Marina

    Marina Guest

    On Wed, 28 Jul 2004 17:29:36 -0400, Victor Bazarov
    <> wrote:

    >sn-us!sn-xit-06!sn-xit-08!sn-xit-11!supernews.com!green.octanews.net!news-out.octanews.net!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.dllstx09.us.to.verio.net!news.verio.net!newsread1.dllstx09.us.to.verio.net.POSTED!53ab2750!not-for-mail
    >From: Victor Bazarov <>
    >User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.6) Gecko/20040113
    >X-Accept-Language: en-us, en
    >MIME-Version: 1.0
    >Newsgroups: comp.lang.c++
    >Subject: Re: Why Doesn't C++ Accept This OFSTREAM VECTOR?
    >References: <>
    >In-Reply-To: <>
    >Content-Type: text/plain; charset=us-ascii; format=flowed
    >Content-Transfer-Encoding: 7bit
    >Lines: 40
    >Message-ID: <cVUNc.195$09.us.to.verio.net>
    >Date: Wed, 28 Jul 2004 17:29:36 -0400
    >NNTP-Posting-Host: 149.65.2.103
    >X-Complaints-To:
    >X-Trace: newsread1.dllstx09.us.to.verio.net 1091050184 149.65.2.103 (Wed, 28 Jul 2004 21:29:44 GMT)
    >NNTP-Posting-Date: Wed, 28 Jul 2004 21:29:44 GMT
    >Organization: NTT/VERIO
    >Xref: sn-us comp.lang.c++:806351
    >
    >Marina wrote:
    >> I get an "access violation" when I use someting like this:
    >>
    >> @@@@@@@@@@@@@@
    >> string tempo;
    >> const char *output;
    >> vector <ofstream> outs(3);

    >
    >This shouldn't compile. 'ofstream' doesn't satisfy the requirement for
    >elements of 'vector': it is not "copy-constructible".
    >
    >> ...
    >> ...
    >> open_output=(const char *)tempo.c_str();

    >
    >There is no need to cast it. 'c_str()' already returns const char*.
    >
    >> ofstream out0(open_output, ios::eek:ut | ios::eek:ut);

    > ^^^^^^^^^^^^^^^^^^^^^^ Huh?
    >> outs[0]=out0;

    >
    >There is no copy semantics for ofstream. This simply shouldn't compile.
    >
    >Why can't you simply say
    >
    > outs[0].open(open_output);
    >
    >>
    >> @@@@@@@@@@@@@@
    >> This access violation comes only when
    >> the line
    >>
    >> outs[0]=out0;
    >>
    >> is included.
    >> Please help. I do need a vector ot "ofstream" type.

    >
    >You can't have it. Create a vector of pointers to ofstream.
    >

    Thank you, Bazarov!
    It worked fine.

    Later...

    Marina
     
    Marina, Jul 28, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrea Desole
    Replies:
    2
    Views:
    916
    Andrea Desole
    Aug 31, 2005
  2. Ingo Nolden
    Replies:
    1
    Views:
    476
    Rob Williscroft
    Jul 14, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,003
    Smokey Grindel
    Dec 2, 2006
  4. Squid Seven

    ofstream * vs. ofstream

    Squid Seven, Jul 13, 2005, in forum: C++
    Replies:
    5
    Views:
    603
    Ivan Johansen
    Jul 14, 2005
  5. Replies:
    8
    Views:
    1,930
    Csaba
    Feb 18, 2006
Loading...

Share This Page