1

I declared a class and then an object, but when I want to use my object, it gives me an error. I searched a bit on Google, but didn't understand what my problem is. The way I declared my class / object is the same as I usually do, and this is the first time it doesn't work.

Here's my .cpp:

#include <iostream>
#include "game.h++"

int game_loop(void)
{
    char *nam;
    Hero *myHero = new Hero();

    printf("So what's your name ? : ");
    scanf("%s", &nam);
    myHero->name(nam); //the error is here
}

and my .h++:

#include <string>

class Hero
{
    public:
        std::string name;
        int race;
        int classe;
        int level = 0;
        int hp = 10;
        int mana = 5;
};
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Tsirsuna
  • 130
  • 2
  • 16
  • 1
    please include the error message in the question – 463035818_is_not_an_ai Sep 23 '21 at 12:31
  • 3
    what do you expect `myHero->name(nam);` to do? `std::string` indeed does not have an `operator()` – 463035818_is_not_an_ai Sep 23 '21 at 12:32
  • it's in the title but i'll add it in the post – Tsirsuna Sep 23 '21 at 12:32
  • @463035818_is_not_a_number i want to convert my char * that scanf gives me into a string and i saw on google that one of the way to do it was like that – Tsirsuna Sep 23 '21 at 12:33
  • 1
    The error message says you are treating `myHero->name` like a function, but it is neither a function nor a function-like class. – aschepler Sep 23 '21 at 12:33
  • 3
    do you want to assign `nam` to `hero->name`? That would be `hero->name = nam;`. You should read about constructors and stay away from `new` and raw pointers. `char* nam` should be `std::string name;` and `Hero *myHero = new Hero();` should be `Hero myHero;` – 463035818_is_not_an_ai Sep 23 '21 at 12:34
  • so i can put a std::string in scanf ? – Tsirsuna Sep 23 '21 at 12:35
  • 4
    [OT]: Better to not use `new` here: `Hero myHero; std::cin >> myHero.name;` would do the job. – Jarod42 Sep 23 '21 at 12:35
  • You probably should not use scanf or printf – drescherjm Sep 23 '21 at 12:35
  • 2
    @SirTristanus You misunderstood the example. You could *create* a string with `std::string name(nam)`, but you can't assign to it like that. To assign a value, you use assignment; `myHero->name = nam;`. Get yourself a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) - learning by googling is extremely time consuming. (And stay away from C.) – molbdnilo Sep 23 '21 at 12:35
  • 1
    no, i just forgot to mention: Don't mix C i/o in your C++ code, unless you know what you are doing. Use `std::cin` – 463035818_is_not_an_ai Sep 23 '21 at 12:35
  • ok ok thanks a lot guys i understood my error ! – Tsirsuna Sep 23 '21 at 12:36
  • 1
    You may want this: [https://en.cppreference.com/w/cpp/string/basic_string/getline](https://en.cppreference.com/w/cpp/string/basic_string/getline) if you use getline please read this: [https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) because you may want to use it and also use `cin >> someVariable` – drescherjm Sep 23 '21 at 12:37
  • I'm surprised nobody mentioned the Undefined Behavior in this code due to using `scanf()` with an uninitialized `char*` pointer. `scanf("%s")` does not allocate memory, it must be given a pointer to a pre-existing buffer to fill in, eg: `char nam[256]; scanf("%256s", nam);`. Otherwise, some platforms support a non-standard `"%ms"` extension to let `scanf()` allocate a new buffer, eg: `char *nam; scanf("%ms", &nam); ... free(nam);` – Remy Lebeau Sep 23 '21 at 15:45

1 Answers1

2

Well, first thing's first, you are trying to use the operator() of a member of type std::string. That operator indeed does not exist.

If you want to set the value of hero->name to nam, you'd have to do something like this:

hero->name = std::string(nam);

Second - why are you using C methods in C++? Using scanf() to input strings is unsafe. You should use fgets(), or C++'s own std::cin.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
alonkh2
  • 533
  • 2
  • 11