declaring a stream then defining it

Discussion in 'C++' started by Gary Wessle, Aug 23, 2006.

  1. Gary Wessle

    Gary Wessle Guest

    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
     
    Gary Wessle, Aug 23, 2006
    #1
    1. Advertising

  2. Gary Wessle

    Heinz Ozwirk Guest

    "Gary Wessle" <> schrieb im Newsbeitrag
    news:...
    > 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
     
    Heinz Ozwirk, Aug 23, 2006
    #2
    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. johny smith
    Replies:
    8
    Views:
    449
    Peter Koch Larsen
    Jul 2, 2004
  2. Sriram Chadalavada

    Defining/declaring constants in Python

    Sriram Chadalavada, Jul 28, 2003, in forum: Python
    Replies:
    2
    Views:
    4,311
    Michael Hudson
    Jul 29, 2003
  3. vlsidesign
    Replies:
    2
    Views:
    244
    Barry
    Jan 11, 2007
  4. Jim
    Replies:
    9
    Views:
    636
    John Harrison
    Mar 5, 2007
  5. Replies:
    0
    Views:
    339
Loading...

Share This Page