[Linker error] undefined reference to...

Please support our C++ advertiser: Download Intel® Parallel Studio Eval
Thread Solved

Join Date: Nov 2009
Posts: 12
Reputation: bamcclur is an unknown quantity at this point 
Solved Threads: 3
bamcclur bamcclur is offline Offline
Newbie Poster

[Linker error] undefined reference to...

 
0
  #1
Nov 21st, 2009
  1. ////////////////////////
  2. /////counter.h file/////
  3. ////////////////////////
  4. #ifndef _COUNTER_H
  5. #define _COUNTER_H
  6. class Counter{
  7.  
  8. private:
  9. int counter;
  10. int limit;
  11. static int nCounters;
  12. public:
  13. Counter(int arg, int arg);
  14. void increment();
  15. void decrement();
  16. int getValue();
  17. static int getNCounters();
  18. };
  19. #include "counter.cpp"
  20. #endif
  21.  
  22. //////////////////////////
  23. /////counter.cpp file/////
  24. //////////////////////////
  25. #include <string>
  26. #include "counter.h"
  27.  
  28. Counter::Counter(int a, int b){
  29. counter=a;
  30. limit=b;
  31. if (&nCounters==NULL){
  32. nCounters=0;
  33. }
  34. nCounters++;
  35. }
  36.  
  37. void Counter::increment()
  38. {
  39. if (counter<limit) counter++;
  40.  
  41. }
  42. void Counter::decrement()
  43. {
  44. if(counter>0) counter--;
  45. }
  46.  
  47. int Counter::getValue(){
  48. return counter;
  49. }
  50.  
  51. int Counter::getNCounters(){
  52. int dummy=nCounters;
  53. return dummy;
  54. }

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
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 255
Reputation: sfuo is on a distinguished road 
Solved Threads: 29
sfuo's Avatar
sfuo sfuo is offline Offline
Posting Whiz in Training
 
1
  #2
Nov 21st, 2009
Add this line of code into your "counter.pp" file

  1. int Counter::nCounters = 0;
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 136
Reputation: zortec is an unknown quantity at this point 
Solved Threads: 15
zortec zortec is offline Offline
Junior Poster
 
1
  #3
Nov 21st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 1,271
Reputation: jonsca is just really nice jonsca is just really nice jonsca is just really nice jonsca is just really nice 
Solved Threads: 145
Sponsor
jonsca jonsca is offline Offline
Nearly a Posting Virtuoso
 
1
  #4
Nov 21st, 2009
Pop int Counter::nCounters; at the top of your counter.cpp file.
http://www.cs.loyola.edu/~lawrie/CS3...assMembers.htm

Also, you don't need to include your cpp file in your h file.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 1,271
Reputation: jonsca is just really nice jonsca is just really nice jonsca is just really nice jonsca is just really nice 
Solved Threads: 145
Sponsor
jonsca jonsca is offline Offline
Nearly a Posting Virtuoso
 
0
  #5
Nov 21st, 2009
Wow that's like a 3-way tie there...
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 96
Reputation: tkud is an unknown quantity at this point 
Solved Threads: 17
tkud's Avatar
tkud tkud is offline Offline
Junior Poster

re

 
0
  #6
Nov 21st, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 12
Reputation: bamcclur is an unknown quantity at this point 
Solved Threads: 3
bamcclur bamcclur is offline Offline
Newbie Poster
 
0
  #7
Nov 21st, 2009
Thanks for the help.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 597 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC