Mail Archives: djgpp/1996/10/22/21:53:23
>>>>> "T" == Tero Parvinen <tero AT mnar DOT tky DOT hut DOT fi> writes:
Tero Parvinen <tero AT mnar DOT tky DOT hut DOT fi> writes:
T> I can't get djgpp to recognize static member variables. If I do a
T> 'gcc -c file.cc' everything seems to run fine, but if I try to
T> link the object files, I get messages complaining about undefined
T> references.
You merely *declared* the static variable 'a', but did not *define* it,
i.e. you did not allocate memory for 'a'. After the class declaration
you have to add the following line:
int CTest::a;
Secondly, you forgot to append the ()'s to the test.GetA-calls in the
printf's. Therefore the starting address in memory of the GetA function
gets printed (function names are pointers), and not the value of
'CTest::a', as intended!
This code works fine with me:
#include <stdio.h>
class CTest
{
public:
static int a;
static void SSetA(int p);
void SetA(int p);
int GetA();
};
int CTest::a; // add definition of 'a'
void CTest::SSetA(int p)
{
a = p;
}
void CTest::SetA(int p)
{
a = p;
}
int CTest::GetA()
{
return a;
}
int main(int argc, char *argv[])
{
CTest::SSetA(1);
CTest test;
printf("%u\n", test.GetA());
test.SetA(10); // ^^
printf("%u\n", test.GetA());
// ^^
return 0; // provides defined return value
}
--
Stefan Simbuerger Tel: +49 941 943 2002
Universitaet Regensburg Fax: +49 941 943 3887
e-mail: Stefan DOT Simbuerger AT physik DOT uni-regensburg DOT de
--------------------------------------------------
- Raw text -