By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,454 Members | 1,795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,454 IT Pros & Developers. It's quick & easy.

Problems compiling simple C++ code (also problems with std::string)

P: n/a
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.

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Susan Baker wrote:
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)
Please consider next time posting _real_ code, and use copy-and-paste, do
not just type into the message. You make mistakes that obstruct the real
problem you're trying to get help on.
what the definition (.cpp) file looks like:

#include "BaseHeader.h"

/************************************************** **/
/* Constructors/Destructor */
/************************************************** **/

MyClass::MyClass(std::string name, int type) {
if (name == NULL) {
'name' is not a pointer. It's pointless to compare it to NULL. What are
you trying to accomplish?
this.name = new std::string(name);
'this' is a pointer. You can't use the "dot" with it. You have to use
the "arrow":

this->name = new std::string(name);

However, if your member 'name' is not a pointer, you shouldn't attempt
using 'new' here. Just do

this->name = name;
this.type = type;
Same note here about the dot and the arrow...
}
}

...... 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.
std::string is not a pointer, it cannot be "initialised to NULL". And
there is nothing to 'delete' if you don't allocate it using 'new'.
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?
Don't. Forget about 'delete' until you learn to use 'new'.

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?
You can call 'empty' member function:

if (name.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?.


A lot. Go back to your favourite C++ book. Try to get Java out of your
mind before you go back to write C++ code.

V
Jul 23 '05 #2

P: n/a
> /************************************************** **/
/* Constructors/Destructor */
/************************************************** **/

MyClass::MyClass(std::string name, int type) {
if (name == NULL) {
this.name = new std::string(name);
this.type = type;
}
}
Looks like what you really want is to pass a _pointer_ to a std::string
and not the string itself, like so:

MyClass::MyClass(std::string* name, int type) : type_(type) {
if (!name) this->name = name; // name is of type std::string*
}

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?
std::string is not a pointer type. It is a class. You can't
initialize it to a pointer value. Perhaps you wanted the default
constructor, like so:

MyClass::MyClass(int type, const std::string& name = std::string()) :
name_(name), type_(type) {}
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?
You can't typically compare something of type T to something of T*
without explicitly writing your own code (and that still wouldn't be
good design). It seems you might want this:

MyClass::MyClass(std::string& name, int type) : type_(type) {
if (!name.empty()) this->name = name;
}
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?.


this is of type MyClass* and not MyClass. You need to use the arrow.
Still, you would use initializers and not assignments to set your
variables (as shown above).

--
I am only a mirage.
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.