Can I not use static const when declaring members?
-
Can I just check, is this code valid? The compiler claims there's a syntax error - 'constant' - for each member. It claims the same even when they are just regular ints too! class MyClass { public: MyClass(){}; ~MyClass(){}; public: static const int MEMBER_ONE; static const int MEMBER_TWO; //... etc. }; const int MyClass::MEMBER_ONE = 64; const int MyClass::MEMBER_TWO = 128; and static const MyClass MyObj; // One instance, declared in header I should note that I'm #including this file into another header, so that class can have a reference to MyObj as a member. Obseve everything, remember more...
-
Can I just check, is this code valid? The compiler claims there's a syntax error - 'constant' - for each member. It claims the same even when they are just regular ints too! class MyClass { public: MyClass(){}; ~MyClass(){}; public: static const int MEMBER_ONE; static const int MEMBER_TWO; //... etc. }; const int MyClass::MEMBER_ONE = 64; const int MyClass::MEMBER_TWO = 128; and static const MyClass MyObj; // One instance, declared in header I should note that I'm #including this file into another header, so that class can have a reference to MyObj as a member. Obseve everything, remember more...
tom76 wrote: const int MyClass::MEMBER_ONE = 64; const int MyClass::MEMBER_TWO = 128; These must appear only once, so move them from the .h to the .cpp that defines the class. It would also help a lot if you included the specific compiler error and indicated what line it referred to. tom76 wrote: static const MyClass MyObj; This instantiating an instance of the class. Is that what you realy want. I don't know if you can have a const class like this. Also globals are bad, very bad. Neville Franks, Author of ED for Windows. www.getsoft.com Make money with our new Affilate program
-
tom76 wrote: const int MyClass::MEMBER_ONE = 64; const int MyClass::MEMBER_TWO = 128; These must appear only once, so move them from the .h to the .cpp that defines the class. It would also help a lot if you included the specific compiler error and indicated what line it referred to. tom76 wrote: static const MyClass MyObj; This instantiating an instance of the class. Is that what you realy want. I don't know if you can have a const class like this. Also globals are bad, very bad. Neville Franks, Author of ED for Windows. www.getsoft.com Make money with our new Affilate program
I've not got a definition (.cpp file) for my class, as all I am using it for is to create a static const object with these members so everything is in MyClass.h . I believe I need a global because I only want one instance of this object, because the members will never change value. The errors are error C2059: syntax error : 'constant' error C2238: unexpected token(s) preceding ';' for each member. Obseve everything, remember more...
-
I've not got a definition (.cpp file) for my class, as all I am using it for is to create a static const object with these members so everything is in MyClass.h . I believe I need a global because I only want one instance of this object, because the members will never change value. The errors are error C2059: syntax error : 'constant' error C2238: unexpected token(s) preceding ';' for each member. Obseve everything, remember more...
tom76 wrote: I believe I need a global because I only want one instance of this object, because the members will never change value. Would it be better to make your variables (which seem to be constants) constant members of the Application-class? So you can access them (almost) everywhere in your program and yet they are not global. Example:
Yourapp.h
class CYourapp : CApp
{
[...]
public:
const int m_One;
const int m_Two;
[...]
}Yourapp.cpp
[...]
CYourapp::m_One = 1;
CYourapp::m_Two = 2;
[...]Use in either of this ways:
int i = theApp.m_One;
int j = static_cast(AfxGetApp())->m_Two;
My opinions may have changed, but not the fact that I am right.
-
tom76 wrote: I believe I need a global because I only want one instance of this object, because the members will never change value. Would it be better to make your variables (which seem to be constants) constant members of the Application-class? So you can access them (almost) everywhere in your program and yet they are not global. Example:
Yourapp.h
class CYourapp : CApp
{
[...]
public:
const int m_One;
const int m_Two;
[...]
}Yourapp.cpp
[...]
CYourapp::m_One = 1;
CYourapp::m_Two = 2;
[...]Use in either of this ways:
int i = theApp.m_One;
int j = static_cast(AfxGetApp())->m_Two;
My opinions may have changed, but not the fact that I am right.
-
Thanks, I'll have a go at that I think. Still don't know why I am getting these syntax errors though. Oh well. Obseve everything, remember more...
At first sight, It's a little strange. 'Cause my poor skill :confused: Why const variable have to be a static one? My wild guess is that a const variable will also have just a one instance between several class instance like a static. Actually, It's not exactly right. But I think const and static have similitiy on each other in this case. Develope yourself
-
Can I just check, is this code valid? The compiler claims there's a syntax error - 'constant' - for each member. It claims the same even when they are just regular ints too! class MyClass { public: MyClass(){}; ~MyClass(){}; public: static const int MEMBER_ONE; static const int MEMBER_TWO; //... etc. }; const int MyClass::MEMBER_ONE = 64; const int MyClass::MEMBER_TWO = 128; and static const MyClass MyObj; // One instance, declared in header I should note that I'm #including this file into another header, so that class can have a reference to MyObj as a member. Obseve everything, remember more...
class MyClass { public: MyClass() : MEMBER_ONE(64), MEMBER_TWO(128){}; ~MyClass(){}; public: const int MEMBER_ONE; const int MEMBER_TWO; //... etc. }; Trust in the code Luke. Yea right!
-
class MyClass { public: MyClass() : MEMBER_ONE(64), MEMBER_TWO(128){}; ~MyClass(){}; public: const int MEMBER_ONE; const int MEMBER_TWO; //... etc. }; Trust in the code Luke. Yea right!