0
////////////////////////
/////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

5
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by bamcclur
Featured Replies
  • 1
    sfuo 111   7 Years Ago

    Add this line of code into your "counter.pp" file [CODE]int Counter::nCounters = 0;[/CODE] Read More

  • 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. Read More

  • 1
    jonsca 1,059   7 Years Ago

    Pop [icode] int Counter::nCounters;[/icode] at the top of your counter.cpp file. [url]http://www.cs.loyola.edu/~lawrie/CS301/F03/StaticClassMembers.htm[/url] Also, you don't need to include your cpp file in your h file. Read More

1

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.

0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.