Error while using static int for incremental record.
-
here in the code below i am trying to print the increment every time the same object is called, but it's giving error.
#include
using namespace std;class Names{
int m;
static int count;
public:
void setcode(void){
m = ++count;
cout<Error:
Quote:
/tmp/ccDcxr5a.o: In function Names::setcode()':
savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0xe): undefined reference toNames::count'
savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0x17): undefined reference to Names::count'
savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0x1d): undefined reference toNames::count'
collect2: error: ld returned 1 exit statusThank you
-
here in the code below i am trying to print the increment every time the same object is called, but it's giving error.
#include
using namespace std;class Names{
int m;
static int count;
public:
void setcode(void){
m = ++count;
cout<Error:
Quote:
/tmp/ccDcxr5a.o: In function Names::setcode()':
savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0xe): undefined reference toNames::count'
savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0x17): undefined reference to Names::count'
savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0x1d): undefined reference toNames::count'
collect2: error: ld returned 1 exit statusThank you
-
Tarun Jha wrote:
but it's giving error.
And we must guess what that error is, and where it occurs?
this is what compiler is showing:
Quote:
/tmp/ccDcxr5a.o: In function Names::setcode()':
savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0xe): undefined reference toNames::count' savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0x17): undefined reference to Names::count'
savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0x1d): undefined reference toNames::count' collect2: error: ld returned 1 exit status -
this is what compiler is showing:
Quote:
/tmp/ccDcxr5a.o: In function Names::setcode()':
savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0xe): undefined reference toNames::count' savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0x17): undefined reference to Names::count'
savinglistOfNames.cpp:(.text._ZN5Names7setcodeEv[_ZN5Names7setcodeEv]+0x1d): undefined reference toNames::count' collect2: error: ld returned 1 exit status -
You have declared the variable
count
in yourNames
class, but never defined it in your code. Add the following line after the class declaration:int Names::count;