-4

I would like to use a string before .getName()

Enemy Troll(Troll, 250, 30); 

string enemyName;
enemyName = Troll;

enemyName.getName(); //this is causing the error... "No member named 'setHP' in 'std::__1::basic_string<char>'"

I want to be able to get the same results as using Troll.getName(); but instead use a string.

  • 3
    "this is causing the error" What error? Please provide a [mcve]. – n. m. could be an AI Jan 28 '18 at 09:29
  • all I am trying to do is use a string on my .getName(); – Matthaeus Gaius Caesar Jan 28 '18 at 09:30
  • No member named 'setHP' in 'std::__1::basic_string' – Matthaeus Gaius Caesar Jan 28 '18 at 09:30
  • `std::string` doesn't have such a function `setHP()`? What makes you think this is related to your `getName()` function? –  Jan 28 '18 at 09:32
  • 3
    you are asking this in a confusing way. What you want is a mapping from "Obama" to a specific instance of the `Enemy` class. If you want that then you need to store that mapping somewhere. For example in a `std::map` – PeterT Jan 28 '18 at 09:33
  • Alright you guys I really appreciate your time, but I have tried looking this simple question up for a while now. I just need to know how to use a string before my .getName. I do not know how to ask this in any other way. – Matthaeus Gaius Caesar Jan 28 '18 at 09:33
  • Edit necessary info into the question. – user202729 Jan 28 '18 at 09:33
  • What is "your getName"? I don't see anything like that in the code. **Complete** example please. – user202729 Jan 28 '18 at 09:34
  • If SE don't allow you to post the whole code, make it a **minimal** example. – user202729 Jan 28 '18 at 09:34
  • You can't use a string there because `std::string` doesn't have such a member function. Pick an item from the [C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Jan 28 '18 at 09:35
  • I am sorry I knew this would confuse people. I just need to know the syntax necessary for using a string before .getName(); – Matthaeus Gaius Caesar Jan 28 '18 at 09:35
  • @MatthewHamilton it doesn't work that way, the variable name is not stored anywhere after you compile your program, you need to store and associate that yourself. – PeterT Jan 28 '18 at 09:36
  • 2
    **What is `getName`?** - **Complete** example please. – user202729 Jan 28 '18 at 09:36
  • If I posted the whole code it would confuse people more I think. I just need to know how to use a string before a .getName(); I feel really bad for using up everyones times. I apologize, I should ask my teacher but I doubt he will understand. – Matthaeus Gaius Caesar Jan 28 '18 at 09:38
  • What you are asking is impossible. I think PeterT has the best guess of the real problem you are tring to solve. – john Jan 28 '18 at 09:38
  • That's why you should post minimal example. The smallest code that describes your issue. – user202729 Jan 28 '18 at 09:38
  • @MatthewHamilton "_If I posted the whole code it would confuse people more I think._" Where did we ask to post the whole code? All we asked is for you to make [mcve]. – Algirdas Preidžius Jan 28 '18 at 09:39
  • Ok then. How do you expect the `getName` to work? You can't `getName` a `string`. – user202729 Jan 28 '18 at 09:43
  • 1
    So just call `Obama.getName()`. What's the problem? A string is not something you can `getName`d. – user202729 Jan 28 '18 at 09:44
  • @user202729 How do people use cin>>(insert string here) to call on a (insert string here).getName(); then? You have to be able to get the name from a user input at some point so how do people do that using a string if something is already declared. Lets say "John" is already in the data base. How will the user be able to input something into the console and that input be converted into a string which will be used right before the .getName(); – Matthaeus Gaius Caesar Jan 28 '18 at 09:46
  • @user202729 Thank you, but how do people use strings that are imputed by users to call upon Obama? Say they already declared it and want to access his information. Wouldn't you use a cin>> and a string to get the user input? Thank you again. – Matthaeus Gaius Caesar Jan 28 '18 at 09:48
  • 1
    @MatthewHamilton It's completely unclear what you are asking. What has `std::cin` to do with that now? What database are you talking about? –  Jan 28 '18 at 09:49
  • @MatthewHamilton `std::string` doesn't have a method named `getName`. Hence, people aren't using it. It's still unclear, what do you expect your `getName` to do. Here's a link to the [documentation of `std::string](http://en.cppreference.com/w/cpp/string/basic_string), with all available methods listed, since it is obvious you couldn't find on your own. – Algirdas Preidžius Jan 28 '18 at 09:53
  • 1
    @MatthewHamilton I hope this question gets closed and deleted soon. That's nothing useful for anyone who will research in future about an unclear problem. –  Jan 28 '18 at 10:03
  • @TheDude I can delete the question if you would recommend that. It was helpful to me and I learned I bit, I just wish I could have asked this in a more clear way. – Matthaeus Gaius Caesar Jan 28 '18 at 10:13
  • @MatthewHamilton No you can't delete the question anymore, since it has upvoted answers now. –  Jan 28 '18 at 10:14
  • @TheDude I tried rewording the question. Hopefully it is slightly more comprehendible. – Matthaeus Gaius Caesar Jan 28 '18 at 10:18
  • @MatthewHamilton No, there's no improvement with your edit. I still don't get what you really want to achieve. –  Jan 28 '18 at 10:26
  • @TheDude I will work on how I format questions on here. I just noticed I made a big mistake in the code above that prob confused many people. I think I fixed it though. I got a little question ban, but I learned from it so thats good c: "It sometimes takes a few attempts at a good question in order to fully learn how our system works, and what the community expects from folks seeking answers. Just do your best to make sure you've improved the quality of your existing questions, and we'll see you in 2 days!" – Matthaeus Gaius Caesar Jan 28 '18 at 10:27

2 Answers2

2

There is no built in way in C++ to use a string to look up a variable of the same name in C++.

What you have to do is create the appropriate data structure for yourself. One way to do that would be to use a map.

#include <map>

std::map<std::string, Enemy> my_map;
...
Enemy trump("Trump", 250, 30);
my_map["Trump"] = trump;
...
std::string name = ...;
Enemy some_enemy = my_map[name];
john
  • 85,011
  • 4
  • 57
  • 81
  • 1
    ... did you understand the question? _wait for OP reaction_ – user202729 Jan 28 '18 at 09:45
  • 1
    @user202729 I took a fair guess, the OP seems a bit incoherent. – john Jan 28 '18 at 09:46
  • Thank you, that answered my question. I am surprised you simply can not use a user imputed string to look up a variable of the same name. I gave you an upvote. – Matthaeus Gaius Caesar Jan 28 '18 at 09:52
  • 1
    @MatthewHamilton It's the nature of C++, variable names chosen by the programmer don't exist in the compiled program and so aren't accessible at run time. This is done for efficiency reasons, but other languages do things differently. – john Jan 28 '18 at 09:54
  • @MatthewHamilton Wait, what please?? –  Jan 28 '18 at 09:54
  • It states that votes cast by someone with less the 15 rep are recorded but not publicly displayed. – Matthaeus Gaius Caesar Jan 28 '18 at 10:00
  • @john That makes sense. I am starting too see why I can not just simply use a string in front of .getName I am a Junior majoring in CS and this is my first class on C++. I am a little ahead on the lessons on Classes, but now I see I am a far bit away from being able to accomplish what I am trying to accomplish, so I will wait till I have finished the course. I have nothing against Obama I am trying to get my brother to switch to CS so I make little games for him to play and modify hoping he will take a liking to the idea of CS. I won't be able to present this to my brother by tomorrow. – Matthaeus Gaius Caesar Jan 28 '18 at 10:06
1

You can store all the instances by name as a static data member in a map for example. But there should be better ways to do it depending on the complete design of your program. But since you didn't provide all the context I'm just going to show a generic example. With some issues like non thread safety, missing null checks, not dealing with duplicates, etc.

#include <iostream>
#include <string>
#include <map>
using namespace std;

class Enemy{
    public:
    std::string m_name;
    int m_hp;
    int m_dmg;
    static std::map<std::string,Enemy*> s_instances;

    Enemy(const std::string& name, int hp, int dmg)
    : m_name(name),
      m_hp(hp),
      m_dmg(dmg)
    {
        s_instances[name] = this;
    }
    ~Enemy()
    {
        s_instances.erase(m_name);
    }

    const std::string& getName() const
    {
        return m_name;
    }
    static Enemy* getInstanceByName(const std::string& name)
    {
        Enemy* result = nullptr;
        auto iter = s_instances.find(name);
        if(iter!=s_instances.end()) result = iter->second;
        return result;
    }

};

std::map<std::string,Enemy*> Enemy::s_instances;

void doStuff()
{
    Enemy* instance = Enemy::getInstanceByName("Ork");
    std::cout << instance->getName();
}

int main() {
    Enemy ork("Ork",300,20);
    doStuff();
    return 0;
}
PeterT
  • 7,981
  • 1
  • 26
  • 34