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

Discussion in 'C++' started by Susan Baker, Jun 24, 2005.

  1. Susan Baker

    Susan Baker Guest

    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.
    Susan Baker, Jun 24, 2005
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Jun 24, 2005
    #2
    1. Advertising

  3. Susan Baker

    kelvSYC Guest

    > /****************************************************/
    > /* 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.
    kelvSYC, Jun 26, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,301
    Ivan Vecerina
    Mar 17, 2005
  2. Vinu
    Replies:
    4
    Views:
    356
    Jim Langston
    Jul 7, 2005
  3. Fei Liu
    Replies:
    9
    Views:
    441
  4. Jeffrey Walton
    Replies:
    10
    Views:
    938
    Mathias Gaunard
    Nov 26, 2006
  5. knipknap
    Replies:
    2
    Views:
    978
    Terry Reedy
    Jan 13, 2010
Loading...

Share This Page