| | |
[Linker error] undefined reference to...
Thread Solved |
•
•
Join Date: Nov 2009
Posts: 12
Reputation:
Solved Threads: 3
C++ Syntax (Toggle Plain Text)
//////////////////////// /////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
1
#2 Nov 21st, 2009
Add this line of code into your "counter.pp" file
C++ Syntax (Toggle Plain Text)
int Counter::nCounters = 0;
•
•
Join Date: Sep 2009
Posts: 1,271
Reputation:
Solved Threads: 145
1
#4 Nov 21st, 2009
Pop
http://www.cs.loyola.edu/~lawrie/CS3...assMembers.htm
Also, you don't need to include your cpp file in your h file.
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.
![]() |
Similar Threads
- Another [Linker error] undefined reference to `WinMain@16' (C++)
- [Linker error] undefined reference to `SDL_Init' (C++)
- [Linker error] undefined reference to `ClosePrinter@4' (C++)
- Linker Error (Undefined Reference) (C++)
- [linker error] undefined reference - why am I getting this error? (C++)
- [Linker error] undefined reference to `CQTMovieFile::CQTMovieFile()' (C++)
- What is it meaned [Linker error] undefined reference to `MAPILogon@24' (C++)
- Dev-C++, GLUI linker error, undefined reference (C++)
Other Threads in the C++ Forum
- Previous Thread: pthread_create call fell into a black hole
- Next Thread: Trying to Output Dynamic Array as Matrix
Views: 597 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays assignment beginner binary borland c++ c/c++ calculator char class classes code compile compiler console constructor conversion convert count data delete desktop dll encryption error file forms fstream function functions game givemetehcodez graph homework http iamthwee ifstream input int java lazy link linker list loop looping loops map math matrix memory newbie news number objects output pointer pointers problem program programming project python qt random read recursion recursive reference return search sort sorting spoonfeeding string strings struct student studio system template templates text tree url variable vc++ vector video visual visualstudio win32 window windows winsock wordfrequency wxwidgets






