-1

I'm doing a little project for school and have to create an enumeration with types of monsters and then a function that takes a value and displays the monster type as a string. Here's my bit of code:

enum MonsterType
{
    GHOST,
    DRAGON,
    GHOUL,
    SHRIEKER,
    GRIFFIN,
};

string getTypeName()
{
    int ID;
    cout << "Input Monster ID" << endl;
    cin >> ID;
    return MonsterType(ID);
}

The errors I'm getting are the following:

no suitable constructor exists to convert from "MonsterType" to "std::basic_string<char, std::char_traits<char>, std::allocator<char>>"

and

'return': cannot convert from 'MonsterType' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'

I'm sure there's a little thing I'm missing and not aware of and I'd really appreciate it if you could help me out.

Thank you

Paul
  • 13,042
  • 3
  • 41
  • 59
Kuba Chmiel
  • 69
  • 1
  • 8
  • _I'm sure there's a little conversion I'm not aware of_ What makes you so sure, that there is a conversion between `MonsterType`, and `std::string`? How should it behave? – Algirdas Preidžius Mar 16 '17 at 19:01
  • No, I mean a conversion I could do. It's probably just a small thing I'm missing and not aware of. – Kuba Chmiel Mar 16 '17 at 19:02
  • 3
    Possible duplicate of [How to convert an enum type variable to a string?](http://stackoverflow.com/questions/5093460/how-to-convert-an-enum-type-variable-to-a-string) – msc Mar 16 '17 at 19:03
  • I've answered a lot of these. Search the internet for "stackoverflow c++ enum convert string". – Thomas Matthews Mar 16 '17 at 19:05
  • There are no facilities in the C++ language to convert from enum (integer value) to the identifier name. Identifier names are disregarded during the build phase (except for debug builds, which keep some symbol names). You will have to come up with your own conversion scheme. – Thomas Matthews Mar 16 '17 at 19:07
  • I only found similar topics for C# and Java, no C++ – Kuba Chmiel Mar 16 '17 at 19:08
  • You could look at the following very involved answer here. http://stackoverflow.com/a/31362042/2066459 – Robert Jacobs Mar 16 '17 at 19:09

2 Answers2

0

What you could do is

enum MonsterType
{
    GHOST,
    DRAGON,
    GHOUL,
    SHRIEKER,
    GRIFFIN,
};

string GetName(MonsterType monsterType){
  string monsterNames[] = {"Ghost", "Dragon", "Ghoul", "Shriker", "Griffin"};
  return monsterNames[monsterType];
}
Luci
  • 1,278
  • 9
  • 17
  • 2
    This has the great opportunity to become unsynchronized with the `enum` definitions. Better to use a structure that contains both the `enum` identifier and the text. Search the internet or StackOverflow for examples. – Thomas Matthews Mar 16 '17 at 19:09
  • Thank you very much for you response, but doesn't that kind of defeat the purpose of using an enumeration for the monster types? – Kuba Chmiel Mar 16 '17 at 19:10
  • Yes, it is true, but being a little school project should be enough – Luci Mar 16 '17 at 19:10
0

Both of these errors are saying the same thing.

Your return MonsterType(ID) is getting a new MonsterType, and trying to return it.

The function is prototyped string getTypeName() (should really be string getTypeName(void) if you want to say 'no parameters'), so you are thus trying to convert that new MonsterType variable into a string. The compiler is complaining that it doesn't know how to do this.

The best way to approach this is by creating a list of the textual (string) representations for each of the moster types you've defined, and making a function map between them.

#include <iostream>

#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[-1]))

using namespace std;

enum MonsterType
{
    GHOST,
    DRAGON,
    GHOUL,
    SHRIEKER,
    GRIFFIN,
};

string MonsterNames[] = {
    "Ghost",
    "Dragon",
    "Ghoul",
    "Shrieker",
    "Griffin",
};

string getTypeName()
{
    int ID;

    cout << "Input Monster ID" << endl;

    cin >> ID;

    if (ID < ARRAY_SIZE(MonsterNames)) {
        return MonsterNames[ID];
    }
    return "unknown";
}

int main(void) {
    cout << getTypeName() << endl;
}

An enum is simply a list of 'things', identified by a number. You cannot access the name of that thing as a string, but only as a 'keyword'.

Attie
  • 6,690
  • 2
  • 24
  • 34