////////////////////////
/////counter.h file/////
////////////////////////
#ifndef _COUNTER_H
#define _COUNTER_H
class Counter{

    private:
       int counter;
       int limit;
       static int nCounters;
    public:
        Counter(int arg, int arg);
        void increment();
        void decrement();
        int getValue();
        static int getNCounters();
};
#include "counter.cpp"
#endif

//////////////////////////
/////counter.cpp file/////
//////////////////////////
#include <string>
#include "counter.h"

Counter::Counter(int a, int b){
   counter=a;
   limit=b;
   if (&nCounters==NULL){
   nCounters=0;
   }  
   nCounters++;
}

void Counter::increment()
{
    if (counter<limit) counter++;

 }
void Counter::decrement()
{
  if(counter>0) counter--;
}

int Counter::getValue(){
return counter;
}

int Counter::getNCounters(){
int dummy=nCounters;
return dummy;
}

I'm getting a linker error every time I try to access nCounters.
so i get this result when compiling:
[Linker error] undefined reference to `Counter::nCounters'
[Linker error] undefined reference to `Counter::nCounters'
[Linker error] undefined reference to `Counter::nCounters'
ld returned 1 exit status

Make sure that you include all the necessary files to compile the project. I would also consider if you need the variables to be static, if that is the case you should declare them at the top of the counter.cpp file.

from the replies you have received, I think you should be satisfied, so kindly mark this thread as solved and add some reputation points to those who helped.

"Show me your code and I will tell you who you are.."-Tkud

This question has already been answered. Start a new discussion instead.