Static variable in header file

I have program like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//header.hh
#include <iostream>
using std::ios;
using std::endl;
class A : public B
{
  public: 
    static bool abool;
    static std::ostream xyz;
}

//class.cc
#include <iostream>
#include "header.hh"

using std::ios;
using std::endl;

// i have to define those static variables first to use it 
bool A::abool;
std::ostream A::xyz;


Here, while compilng i get error saying that
undefined reference to A::xyz
but there is no error with A::abool ??

Can anyone explain why this error is there and how to remove it ??
Thanks.
Last edited on
you need to declare all your static variables at the top of .cpp files where you use them.

can u explain it a bit..
i have declared the static variables abool and xyz in cpp
std::ostream doesn't have a constructor that takes no parameters.

http://www.cplusplus.com/reference/iostream/ostream/ostream/
see if you have a static variable in a class lets say

1
2
3
4
5
6
7
8
9
10
//some.h
class some
{

public:
static int m_i;

//some more code

};


then you need to declare that static variable in the .cpp files. like

1
2
3
4
5
//some.cpp

static some::m_i;//you need to do this or else the compiler give error

//here goes the class implementation 

Topic archived. No new replies allowed.