10

I got a qualification error of the member variable 'objectCount'. The compiler also returns 'ISO C++ forbids in-class intialization of non-const static member'. This is the main class:

#include <iostream>
#include "Tree.h"
using namespace std;

int main()
{
    Tree oak;
    Tree elm;
    Tree pine;

    cout << "**********\noak: " << oak.getObjectCount()<< endl;
    cout << "**********\nelm: " << elm.getObjectCount()<< endl;
    cout << "**********\npine: " << pine.getObjectCount()<< endl;
}

This is the tree class which contains the non-const static objectCount:

#ifndef TREE_H_INCLUDED
#define TREE_H_INCLUDED

class Tree
{
    private:
        static int objectCount;
    public:
        Tree()
        {
            objectCount++;
        }
        int getObjectCount() const
        {
            return objectCount;
        }
    int Tree::objectCount = 0;
}
#endif // TREE_H_INCLUDED
ulidtko
  • 14,740
  • 10
  • 56
  • 88
kifcaliph
  • 311
  • 2
  • 4
  • 15
  • There is another alternative that wasn't mentioned in any of the suggested answers at the time of this writing, which allows you to **keep** everything in a **single header**. See the example in [this SO answer](http://stackoverflow.com/a/33618854/3041008), it maps perfectly to your example. – mucaho Nov 09 '15 at 21:53

3 Answers3

17

You have to define the static variable in the source file that includes this header.

#include "Tree.h"

int Tree::objectCount = 0;  // This definition should not be in the header file.
                            // Definition resides in another source file.
                            // In this case it is main.cpp 
Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • there is still the following error 'two or more data types in declaration of objectCount' – kifcaliph Jul 16 '11 at 22:16
  • I forgot to put this line int `Tree::objectCount = 0;` outside the class and I forgot to end the header class with semicolon thank you – kifcaliph Jul 16 '11 at 22:35
  • Why does this variable have to be declared outside of the class? I am trying to define and initialize a private variable, and I am getting this same error. How do I do this outside the class? – dangerChihuahua007 Feb 01 '12 at 03:31
  • @DavidFaux Show what are you trying to do. Only static const integral data members can be initialized within a class. – Mahesh Feb 01 '12 at 03:55
5
int Tree::objectCount = 0;

The above line should be outside the class, and in .cpp file, as shown below:

//Tree.cpp 
#include "Tree.h"

int Tree::objectCount = 0;
Nawaz
  • 353,942
  • 115
  • 666
  • 851
3

You need to define it outside the scope in a single C++ file, not in the header.

int Tree::objectCount = 0;
int main()
{
    Tree oak;
    Tree elm;
    Tree pine;

    cout << "**********\noak: " << oak.getObjectCount()<< endl;
    cout << "**********\nelm: " << elm.getObjectCount()<< endl;
    cout << "**********\npine: " << pine.getObjectCount()<< endl;
}

#ifndef TREE_H_INCLUDED
#define TREE_H_INCLUDED

class Tree
{
    private:
        static int objectCount;
    public:
        Tree()
        {
            objectCount++;
        }
        int getObjectCount() const
        {
            return objectCount;
        }
}
#endif // TREE_H_INCLUDED
Puppy
  • 144,682
  • 38
  • 256
  • 465