I am learning C++ and have a weird issue with a lab I am working on. Where I don't seem to be referencing things right would be my guess.
It basically throws an error like it can't find the static int nCounters declared in the Class Header file.
//Start of counter.h
class Counter
{
private:
int counter;
int limit;
static int nCounters;
public:
Counter(int x, int y);
void increment();
void decrement();
int getValue();
int getNCounters();
};
//end of counter.h
//Start of counter.cpp
#include "counter.h"
Counter::Counter(int x, int y)
{
counter = x;
limit = y;
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;
}
//end of counter.cpp
//Start of counter_test.cpp
#include <iostream>
#include "counter.cpp"
using namespace std;
int main(){
Counter derp(5,5);
cout << derp.getValue();
return 0;
}
//end of counter_test.cpp