Skip to main content

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 

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

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 
Source Link
kifcaliph
  • 331
  • 2
  • 4
  • 15

C++ initialization of non constant static member variable??

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