Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static variable problem

Reply
Thread Tools

static variable problem

 
 
Nasim
Guest
Posts: n/a
 
      02-04-2005
Hi there,

I am little confused with the code below

//afs.cc
#include<fstream>
using std: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

 
Reply With Quote
 
 
 
 
Siemel Naran
Guest
Posts: n/a
 
      02-04-2005
"Nasim" <(E-Mail Removed)> wrote in message

> //afs.cc
> #include<fstream>
> using std: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;



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Usage of Static variable name in static context Vikram Kalra Java 23 09-07-2007 08:51 AM
non-static variable this cannot be referenced from a static context kookey Java 3 08-20-2005 07:43 PM
Does a static variable in a class's member fn always remain static? Sam C++ 4 01-13-2004 11:05 PM
Global static variable vs static method Marcin Vorbrodt C++ 3 09-05-2003 07:52 AM
a static local variable in a static method is thread local storage? Patrick Hoffmann C++ 3 08-08-2003 02:37 PM



Advertisments