I have TestMethods.h
#pragma once // strings and c-strings #include <iostream> #include <cstring> #include <string> class TestMethods { private: static int nextNodeID; // I tried the following line instead ...it says the in-class initializer must be constant ... but this is not a constant...it needs to increment. //static int nextNodeID = 0; int nodeID; std::string fnPFRfile; // Name of location data file for this node. public: TestMethods(); ~TestMethods(); int currentNodeID(); }; // Initialize the nextNodeID int TestMethods::nextNodeID = 0; // I tried this down here ... it says the variable is multiply defined. I have TestMethods.cpp
#include "stdafx.h" #include "TestMethods.h" TestMethods::TestMethods() { nodeID = nextNodeID; ++nextNodeID; } TestMethods::~TestMethods() { } int TestMethods::currentNodeID() { return nextNodeID; } I've looked at this example here: Unique id of class instance
It looks almost identical to mine. I tried both the top solutions. Neither works for me. Obviously I'm missing something. Can anyone point out what it is?
static int nextNodeID = 0;in the class? You can still increment it in your constructor.