Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > declaring a stream then defining it

Reply
Thread Tools

declaring a stream then defining it

 
 
Gary Wessle
Guest
Posts: n/a
 
      08-23-2006
Hi

can I declare an ofstream in a class declaration in .h file and define
it inside a method in the .cpp file? if so, what does the syntax of
defining it looks like.
I tried

....
ofstream out; // in .h file

and

myclass::myfun(){
....
string s = "some.file";
out(s.c_str()); //in .cpp file
....
on the line above, I get
error: no match for call to '(std:fstream) (const char*)'



so that another method uses it

myclass::myotherfun(){
out << ...


thanks
 
Reply With Quote
 
 
 
 
Heinz Ozwirk
Guest
Posts: n/a
 
      08-23-2006
"Gary Wessle" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> Hi
>
> can I declare an ofstream in a class declaration in .h file and define
> it inside a method in the .cpp file? if so, what does the syntax of
> defining it looks like.
> I tried
>
> ...
> ofstream out; // in .h file


This is a definition, not a declaration. A declaration would be

extern ofstream out;

but I suspect you want to define a stream variable (or member variable of
some class) and open it later.

> and
>
> myclass::myfun(){
> ...
> string s = "some.file";
> out(s.c_str()); //in .cpp file


That'd be a call of ofstream:perator()(char const*). If you want to open a
file on that stream, try

out.open(s.c_str());

HTH
Heinz

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring a valarray, then initialising it later Jim C++ 9 03-05-2007 09:25 PM
declaring variable and then also in function prototype -- seems weird vlsidesign C Programming 2 01-11-2007 12:14 PM
Help. SessionID is x then y then x then y BodiKlamph@gmail.com ASP General 0 09-03-2005 03:02 PM
defining or not defining destructors johny smith C++ 8 07-02-2004 08:51 AM
Defining/declaring constants in Python Sriram Chadalavada Python 2 07-29-2003 11:56 AM



Advertisments