メンバー変数 'objectCount'の修飾エラーが発生しました。また、コンパイラは、 'ISO C++では非const staticメンバのインラインクラス初期化を禁じます。 これはメインクラスです:非定数静的メンバー変数のC++初期化?
#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;
}
これは非const静的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
で推奨的回答のいずれかに記載されていなかった別の代替はありあなたが** ** **一つのヘッダー**にすべてを保つことを可能にするこの執筆の時。 [this SO answer](http://stackoverflow.com/a/33618854/3041008)の例を見てください。これはあなたの例に完全に対応しています。 – mucaho