what's the difference betwwen these two ways to use std::ofstream

Discussion in 'C++' started by thinktwice, Aug 24, 2006.

  1. thinktwice

    thinktwice Guest

    //case 1, works fine
    function(..)
    {
    std::eek:fstream out;
    out.open(file);
    out << "error info" <<endl;
    out.close();
    }

    //case 2, declare it in a class
    class Logger
    {
    public:
    Logger(std::wstring file, std::ios_base::eek:penmode mode /*undefined
    too, why?*/){}
    private:
    std::eek:fstream out; //error c2079, uses undefined class
    'std::basic_ofstream<_Elem, _Traits>'
    }

    btw, how to use ios_base:eek:penmode? is it declared in std namespace?
    thinktwice, Aug 24, 2006
    #1
    1. Advertising

  2. thinktwice

    Bo Persson Guest

    "thinktwice" <> skrev i meddelandet
    news:...
    > //case 1, works fine
    > function(..)
    > {
    > std::eek:fstream out;
    > out.open(file);
    > out << "error info" <<endl;
    > out.close();
    > }


    Just a guess: The function is in a cpp file that includes the
    <fstream> header?


    > //case 2, declare it in a class
    > class Logger
    > {
    > public:
    > Logger(std::wstring file, std::ios_base::eek:penmode mode
    > /*undefined
    > too, why?*/){}
    > private:
    > std::eek:fstream out; //error c2079, uses undefined class
    > 'std::basic_ofstream<_Elem, _Traits>'
    > }


    A class declaration if often in a .h file. If so, it must also include
    the necessary headers for names it uses.

    >
    > btw, how to use ios_base:eek:penmode? is it declared in std namespace?
    >


    It is. There is a header <ios> that contains a lot of types and
    constants used by the stream classes.


    Bo Persson
    Bo Persson, Aug 24, 2006
    #2
    1. Advertising

  3. thinktwice

    thinktwice Guest

    thanks Persson , you are right,
    i do include <fstream> in cpp file.
    and the function implementation and the class definination is in the
    header file.
    thinktwice, Aug 25, 2006
    #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. Andy
    Replies:
    5
    Views:
    504
    Shezan Baig
    Jan 30, 2005
  2. Squid Seven

    ofstream * vs. ofstream

    Squid Seven, Jul 13, 2005, in forum: C++
    Replies:
    5
    Views:
    598
    Ivan Johansen
    Jul 14, 2005
  3. Replies:
    9
    Views:
    335
    Karl Heinz Buchegger
    Nov 21, 2005
  4. Solang
    Replies:
    3
    Views:
    321
    Chris M. Thomasson
    Sep 20, 2009
  5. Replies:
    2
    Views:
    88
Loading...

Share This Page