static variable problem

N

Nasim

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
 
S

Siemel Naran

Nasim said:
//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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top