943,095 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1904
  • C++ RSS
Nov 21st, 2009
0

[Linker error] undefined reference to...

Expand Post »
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 11
Solved Threads: 4
Newbie Poster
bamcclur is offline Offline
20 posts
since Nov 2009
Nov 21st, 2009
1
Re: [Linker error] undefined reference to...
Add this line of code into your "counter.pp" file

C++ Syntax (Toggle Plain Text)
  1. int Counter::nCounters = 0;
Reputation Points: 164
Solved Threads: 98
Practically a Master Poster
sfuo is offline Offline
642 posts
since Jul 2009
Nov 21st, 2009
1
Re: [Linker error] undefined reference to...
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.
Reputation Points: 21
Solved Threads: 16
Junior Poster
zortec is offline Offline
144 posts
since Nov 2009
Nov 21st, 2009
1
Re: [Linker error] undefined reference to...
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.
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Nov 21st, 2009
0
Re: [Linker error] undefined reference to...
Wow that's like a 3-way tie there...
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Nov 21st, 2009
0

re

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.
Quote ...
"Show me your code and I will tell you who you are.."-Tkud
Reputation Points: 13
Solved Threads: 46
Posting Whiz in Training
tkud is offline Offline
235 posts
since Sep 2009
Nov 21st, 2009
0
Re: [Linker error] undefined reference to...
Thanks for the help.
Reputation Points: 11
Solved Threads: 4
Newbie Poster
bamcclur is offline Offline
20 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: pthread_create call fell into a black hole
Next Thread in C++ Forum Timeline: Trying to Output Dynamic Array as Matrix





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC