static variable problem

Discussion in 'C++' started by Nasim, Feb 4, 2005.

  1. Nasim

    Nasim Guest

    Hi there,

    I am little confused with the code below

    //afs.cc
    #include<fstream>
    using std::eek:fstream;

    struct A {
    static ofstream fout;
    };

    int main() {
    A::fout.open("a.dat");
    A::fout.close();
    return 0;
    }

    I think the code is ok. And certainly it compiles (g++ -c afs.cc). But
    it fails when I wanted the executable (g++ -o afs afs.cc) and gives the
    following error

    In function 'main':
    : undefined reference to 'Test::fout'
    collect2: ld returned 1 exit status

    I am really surprised with the happening. Please indicate what I am
    missing.

    Thank you.

    Nasim
     
    Nasim, Feb 4, 2005
    #1
    1. Advertising

  2. Nasim

    Siemel Naran Guest

    "Nasim" <> wrote in message

    > //afs.cc
    > #include<fstream>
    > using std::eek:fstream;
    >
    > struct A {
    > static ofstream fout;
    > };


    Don't forget to define A::fout. What you have above is just a declaration
    of fout. Just add the following

    ofstream A::fout;
     
    Siemel Naran, Feb 4, 2005
    #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. kookey
    Replies:
    3
    Views:
    20,809
    Andrew Thompson
    Aug 20, 2005
  2. Patrick Hoffmann
    Replies:
    3
    Views:
    2,922
    Christian Jan├čen
    Aug 8, 2003
  3. Marcin Vorbrodt

    Global static variable vs static method

    Marcin Vorbrodt, Sep 5, 2003, in forum: C++
    Replies:
    3
    Views:
    5,449
    Denis Perelyubskiy
    Sep 5, 2003
  4. Sam
    Replies:
    4
    Views:
    491
    The Directive
    Jan 13, 2004
  5. Vikram Kalra
    Replies:
    23
    Views:
    905
    Nigel Wade
    Sep 7, 2007
Loading...

Share This Page