Thursday, July 9, 2009

When I try to change a Static int value it gets an error saying it's undefined from Dev-C++?

class A


{


static int i;


A()


{


i++;


}


~A()


{


i--;


}


}





when I try to use the class as a varible like this


A AClass;





it get an error like this twice





(linker error) undefined reference to i

When I try to change a Static int value it gets an error saying it's undefined from Dev-C++?
Yep, you must initialize it. If you don't initialize it, it first complains because it doesn't know how to increment the variable. Then it will complain when it hits the decrement operator. Since this is a class and those statements would be called as the constructor and destructor are called, this will be called as well. You can create a static without initialization in most cases, but leaving it blank here makes it undefined behavior if the compiler trys to increment or decrement it.
Reply:doesnt i have to have a first initial value?

online florist

No comments:

Post a Comment