How to declare a static fstream within a class?

Discussion in 'C++' started by Javi, Oct 10, 2005.

  1. Javi

    Javi Guest

    Hi!. I'm trying to do something like this but results in lot of errors.
    Can anybody tell me how to do it (first of all I hope you can grasp my
    idea)?:

    myfile.cpp
    #include <iostream>
    #include <fstream>

    using namespace std;

    class debuginformation{

    public:

    static fstream outfile;
    static void message001(int n);
    static void message002(double alpha);
    };

    fstream debuginformation::eek:utfile("debug.dat"); //Is this rigth
    // Maybe I should write fstream
    debuginformation::eek:utfile=fstream("debug.dat");?

    void debuginformation::message001(int n){outfile<<"The var n
    is"<<n<<endl;}


    int main(){
    // Do I have to declare any objects of class debuginformation?
    // I only need to write the various static messages to the file
    outfile.

    int n=10;
    debuginformation::message001(n);

    }


    If I compile this I get lot of errors complaining about:

    undefined reference to `std::basic_ostream<char,
    std::char_traits<char> >& std::eek:perator<< <std::char_traits<char>
    >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'


    In methods involving this structure: outfile << anything.


    Well I hope you can understand what I mean, maybe I've not been very
    clear.

    Thanks,

    Javi.
    Javi, Oct 10, 2005
    #1
    1. Advertising

  2. On 10 Oct 2005 04:03:08 -0700, "Javi" <>
    wrote:

    >Hi!. I'm trying to do something like this but results in lot of errors.
    >Can anybody tell me how to do it (first of all I hope you can grasp my
    >idea)?:
    >
    >myfile.cpp
    >#include <iostream>
    >#include <fstream>
    >
    >using namespace std;
    >
    >class debuginformation{
    >
    >public:
    >
    >static fstream outfile;
    >static void message001(int n);
    >static void message002(double alpha);
    >};
    >
    >fstream debuginformation::eek:utfile("debug.dat"); //Is this rigth
    >// Maybe I should write fstream
    >debuginformation::eek:utfile=fstream("debug.dat");?
    >
    >void debuginformation::message001(int n){outfile<<"The var n
    >is"<<n<<endl;}
    >
    >
    >int main(){
    >// Do I have to declare any objects of class debuginformation?
    >// I only need to write the various static messages to the file
    >outfile.
    >
    >int n=10;
    >debuginformation::message001(n);
    >
    >}
    >
    >
    >If I compile this I get lot of errors complaining about:
    >
    > undefined reference to `std::basic_ostream<char,
    >std::char_traits<char> >& std::eek:perator<< <std::char_traits<char>
    >>(std::basic_ostream<char, std::char_traits<char> >&, char const*)'

    >
    >In methods involving this structure: outfile << anything.
    >
    >
    >Well I hope you can understand what I mean, maybe I've not been very
    >clear.
    >
    > Thanks,
    >
    >Javi.


    Once I fixed up the line endings, and added an implementation for
    message002(double), your code compiled fine with the Borland 5.5.
    command-line compiler. Is this the exact same code you are having
    trouble with?

    --
    Bob Hairgrove
    Bob Hairgrove, Oct 10, 2005
    #2
    1. Advertising

  3. Javi

    Javi Guest

    Yes, my exact code is:

    #include <iostream>
    #include <fstream>

    using namespace std;

    class dbginfo{

    public:
    static fstream outfile;
    static void d001(int n);
    static void d002(double d);
    };

    fstream dbginfo::eek:utfile("output.dat");
    void dbginfo::d001(int n){outfile<<"First proof"<<n;}
    void dbginfo::d002(double d){outfile<<"First proof"<<endl;}

    int main(){
    int n=10;
    dbginfo::d001(n);}

    Simple enough, but I do have some compilation problems with gnu g++.

    OK,OK,OK...well I'm so embarrassed... It was a problem with my .o code,
    I didn't remove it after a first failed compilation and afterwards the
    make program took this bad code.

    Thanks a lot Bob for your help and for taking some time to check my
    code, I was going crazy.

    Thanks again,
    Javi.
    Javi, Oct 10, 2005
    #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. Armando
    Replies:
    6
    Views:
    735
    Armando
    Jan 29, 2004
  2. Old Wolf
    Replies:
    1
    Views:
    421
    Victor Bazarov
    May 10, 2005
  3. sharat
    Replies:
    5
    Views:
    377
    John Carson
    Dec 17, 2006
  4. johnmmcparland
    Replies:
    3
    Views:
    492
    Gavin Deane
    Mar 29, 2007
  5. ZelluX
    Replies:
    24
    Views:
    855
    Hendrik Maryns
    Nov 19, 2007
Loading...

Share This Page