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

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

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

Counter::Counter(int a, int b){
   if (&nCounters==NULL){

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

Recommended Answers

All 6 Replies

Add this line of code into your "counter.pp" file

int Counter::nCounters = 0;

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.

Wow that's like a 3-way tie there...

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

Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.