-5

Here's my code:

#include <iostream>
#include <string>

class Human
{
public:
    std::string * name = new std::string();
    void introduce();
};

void Human::introduce()
{
    std::cout << "Hello, my name is " << Human::name << std::endl;
}

int main()
{
    Human * martha;
    martha->name = new std::string("Martha");
    martha->introduce();

    return 0;
}

Well, it's supposed to print a message out like: "Hello, my name is Martha" but it doesn't print neither the "Hello, my name is" string or the "Martha" name. Why does it occur?

Martín Nieva
  • 476
  • 1
  • 5
  • 13
  • 9
    `martha->name` exhibits undefined behavior, by way of accessing an uninitialized variable `martha`. Your program never creates an instance of `Human`. Why are you using pointers everywhere anyway? C++ is not Java. – Igor Tandetnik Jul 09 '18 at 03:49
  • 4
    Please turn on and don't ignore your compiler warnings. It should tell you that `martha` is used before being defined, at least. – Ken Y-N Jul 09 '18 at 03:52
  • I'm learning COCOS2D-X and there is so many pointers in the template that I don't understand. At the end of the day here I am trying to understand them. – Martín Nieva Jul 09 '18 at 04:01
  • There aren't any compiler's warnings, just check this out: http://prntscr.com/k44rk0 – Martín Nieva Jul 09 '18 at 04:03
  • An object of human class is not created. Only a pointer to the class is created. `Human* martha = new Human()` and then execute the program. – Shrikanth N Jul 09 '18 at 04:04
  • 1
    @jafactor You should've missed the "turn on" part of Ken Y-N's comment. –  Jul 09 '18 at 04:05
  • @jafactor [See the warnings here](http://coliru.stacked-crooked.com/a/af3e5c4e88cbc5e2) – PaulMcKenzie Jul 09 '18 at 04:08
  • 2
    @jafactor *I'm learning COCOS2D-X and there is so many pointers in the template that I don't understand* -- SDK's and API's are meant for persons who are already experienced in the language that the API/SDK is targeted for. It isn't meant to be used to learn the basics of C++. – PaulMcKenzie Jul 09 '18 at 04:19
  • Point taken. Thank you all for such detailed responses. I really appreciate it. – Martín Nieva Jul 14 '18 at 06:28

3 Answers3

2

The fix is simple and is to completely remove all pointers; see the code below. There are a number of issues with your code that I could address in detail, including memory leaks, uninitialized variables, and general misuse of pointers, but it seems that you're possibly coming from a different language background and should spend time learning good practice and the important semantics and idioms in modern C++ from a good C++ book.

#include <iostream>
#include <string>

class Human
{
public:
    std::string name;
    void introduce();
};

void Human::introduce()
{
    std::cout << "Hello, my name is " << name << std::endl;
}

int main()
{
    Human martha;
    martha.name = "Martha";
    martha.introduce();

    return 0;
}
alter_igel
  • 6,899
  • 3
  • 21
  • 40
-1

Few modifications are required to the code. Updated code along with the comments to the change made are included below.

#include <iostream>
#include <string>

class Human
{
public:
    //Removed pointer to a string
    //Cannot have an instantiation inside class declaration
    //std::string * name = new std::string();
    //Instead have a string member variable
    std::string name;
    void introduce();
};

void Human::introduce()
{
    //Human::name not required this is a member function
    //of the same class
    std::cout << "Hello, my name is " << name << std::endl; 
}

int main()
{
    Human *martha = new Human();
    //Assign a constant string to string member variable
    martha->name = "Martha";
    martha->introduce();

    return 0;
}

As suggested by @alter igel - The Definitive C++ Book Guide and List would be a good place to start.

Shrikanth N
  • 652
  • 3
  • 17
  • Pretty answer. Why is it constant? For writing a constant don't I need to write the "const" keyword? – Martín Nieva Jul 13 '18 at 00:36
  • Constant string I mean you are giving a complete name and not assigning a variable to it. As we are giving "Martha" and not martha->name = nameOfPerson, where nameOfPerson is a variable that has the actual string. – Shrikanth N Jul 13 '18 at 09:08
-2
#include <iostream>
#include <string>

class Human {
public:
    void Human(std::string* n) { name = n; }
    void introduce();
private:
    std::string* name;
};

void Human::introduce() {
    std::cout << "Hello, my name is " << name << std::endl;
}

int main() {
    Human* martha = new Human(new std:string("Martha"));
    martha->introduce();

    return 0;
}

Try that. The difference is that you don't initialise the variable in the class definition, and you initialise the name with the constructor. You can split the method definition out into it's own section, but it's only one line and is fine being inside the class definition.

Maxim Srour
  • 157
  • 1
  • 17