S
Susan Baker
Hi,
I have declared a class MyClass in a header file MyClass.h
I have then gone onto define the class in MyClass.cpp. This is (roughly)
what the definition (.cpp) file looks like:
#include "BaseHeader.h"
/****************************************************/
/* Constructors/Destructor */
/****************************************************/
MyClass::MyClass(std::string name, int type) {
if (name == NULL) {
this.name = new std::string(name);
this.type = type;
}
}
....... etc ....
First of all, I am having problems with std::string class as follows:
1). I wanted to initialize it to NULL so I could check later, if it
needs to be deleted later. The compiler complained that I could not
initialize it the declaration (header file) - How do I initialize name
to NULL so I can determine if I need to delete it in the destructor?
2). I can't also check for equality, as you can see in the code snippet
above, I don't want to do anything if name is NULL (still using C
pointer parlance here) - How do I check that variable name has been
initialized or is not empty?
3). Most annoying (and inexplicable to me) so far is the fact that in
the constructor shown above, the compiler somehow, does not recognize
the fact that the methods are "bound" to a class, and therefore, forbids
the use of the this keyword. Here is the actual compiler error message:
"MyClass.cpp(33): error C2228: left of '.name' must have
class/struct/union type" - Arrgh!!. Could someone please tell me what
I'm doing wrong?.
Thanks
PS: I'm using Visual C++ 2003 (VS 7.1) for compilation.
I have declared a class MyClass in a header file MyClass.h
I have then gone onto define the class in MyClass.cpp. This is (roughly)
what the definition (.cpp) file looks like:
#include "BaseHeader.h"
/****************************************************/
/* Constructors/Destructor */
/****************************************************/
MyClass::MyClass(std::string name, int type) {
if (name == NULL) {
this.name = new std::string(name);
this.type = type;
}
}
....... etc ....
First of all, I am having problems with std::string class as follows:
1). I wanted to initialize it to NULL so I could check later, if it
needs to be deleted later. The compiler complained that I could not
initialize it the declaration (header file) - How do I initialize name
to NULL so I can determine if I need to delete it in the destructor?
2). I can't also check for equality, as you can see in the code snippet
above, I don't want to do anything if name is NULL (still using C
pointer parlance here) - How do I check that variable name has been
initialized or is not empty?
3). Most annoying (and inexplicable to me) so far is the fact that in
the constructor shown above, the compiler somehow, does not recognize
the fact that the methods are "bound" to a class, and therefore, forbids
the use of the this keyword. Here is the actual compiler error message:
"MyClass.cpp(33): error C2228: left of '.name' must have
class/struct/union type" - Arrgh!!. Could someone please tell me what
I'm doing wrong?.
Thanks
PS: I'm using Visual C++ 2003 (VS 7.1) for compilation.