hi all
i have a question with C++/pgcc 2.91.60 with following code.
hope C++ is not off topic :)
---from here----
#include <iostream.h>
class base {
public:
base(void){counter++;}
~base(void){counter--;}
int get_counter(void){
return counter;
}
private:
static int counter; // (A)
};
int
main (void)
{
base b;
cout << b.get_counter() << endl;
return 0;
}
-----till here----
trying to make a class with a static variable so that all objects
instantiated for that class can share one variable.
# hope i'm on the right road...
Question 1:
how can i initialize counter at (A) to 0?
i replaced (A) with
static int counter = 0;
and compiler said:
"ANSI C++ forbids in-class initialization
of non-const static member `counter'"
Question 2;
it seems that linker is not able to link object files from
the source code:
bash$ g++ -Wall static.cpp
/tmp/ccPYx1jJ.o: In function `base::get_counter(void)':
/tmp/ccPYx1jJ.o(.base::gnu.linkonce.t.get_counter(void)+0x8): undefined reference to
`base::counter'
/tmp/ccPYx1jJ.o: In function `base::~base(void)':
/tmp/ccPYx1jJ.o(.gnu.linkonce.t._._4base+0xd): undefined reference to `base::counter'
/tmp/ccPYx1jJ.o: In function `base::base(void)':
/tmp/ccPYx1jJ.o(.base::gnu.linkonce.t.(void)+0x5): undefined reference to
`base::counter'
collect2: ld returned 1 exit status
regards,
--
yashi