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

M

Marina

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
 
V

Victor Bazarov

Marina said:
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
 
M

Marina

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 <[email protected]>
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: <[email protected]>
In-Reply-To: <[email protected]>
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 40
Message-ID: <[email protected]>
Date: Wed, 28 Jul 2004 17:29:36 -0400
NNTP-Posting-Host: 149.65.2.103
X-Complaints-To: (e-mail address removed)
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
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top