I got a qualification error of the member variable 'objectCount'. theThe compiler also returns 'ISO C++ forbids in-class intialization of non-const static member'. thisThis is the main class #include #include "Tree.h" using namespace std;:
#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; } thisThis 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