Velocity Reviews - Computer Hardware Reviews

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

Thread Tools

static variable problem

Posts: n/a
Hi there,

I am little confused with the code below

using std:fstream;

struct A {
static ofstream fout;

int main() {"a.dat");
return 0;

I think the code is ok. And certainly it compiles (g++ -c But
it fails when I wanted the executable (g++ -o afs 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

Thank you.


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

> //
> #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

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