By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

static variable problem

P: n/a
Hi there,

I am little confused with the code below

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

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

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Nasim" <na*********@gmail.com> wrote in message
//afs.cc
#include<fstream>
using std::ofstream;

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;

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.