I'm having difficulty getting this to compile. I think the it has to do with the static variables, but I'm not 100% sure what I'm doing. Here is the error message I keep getting:
Undefined symbols for architecture x86_64: "Counter::nCounters", referenced from: Counter::Counter(int, int) in main.o
Counter::getNCounters() in main.o
Counter::Counter(int, int) in Counter.o
Counter::getNCounters() in Counter.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Here is the header file:
#ifndef project1_Counter_h
#define project1_Counter_h
class Counter
{
private:
int counter;
int limit;
static int nCounters;
public:
Counter(int, int);
void increment();
void decrement();
int getValue();
static int getNCounters();
};
#endif
And here is the .cpp file:
#include "Counter.h"
Counter::Counter(int a, int b)
{
counter = a;
limit = b;
nCounters++;
}
void Counter::increment()
{
if (counter < limit)
counter++;
}
void Counter::decrement()
{
if (counter > 0)
counter--;
}
int Counter::getValue()
{
return counter;
}
int Counter::getNCounters()
{
return nCounters;
}
And the main.cpp is just a simple Hello World program. Any help would be appreciated.