declaring a stream then defining it

G

Gary Wessle

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::eek:fstream) (const char*)'



so that another method uses it

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


thanks
 
H

Heinz Ozwirk

Gary Wessle said:
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::eek:perator()(char const*). If you want to open a
file on that stream, try

out.open(s.c_str());

HTH
Heinz
 

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top