-1

i know that this question have been answered here already : C++ string to enum but i'm really lost on how to use it so please don't be rude ;)

I want to convert my string in a tEnumCouleur.. I have :

    #pragma once
    #include <map>
    #include <cassert>
    class EnumCouleur
    {
    public :

        enum tEnumCouleur{BLACK,BLUE,RED,GREEN,YELLOW,CYAN};
    std::map<std::string, tEnumCouleur> xmap = boost::assign::map_list_of<std::string, tEnumCouleur>(BLACK, "BLACK")(BLUE, "BLUE")(GREEN, "GREEN");

    //Getting an error with the "=" saying it's an unautorized initialisation 
//also getting an error at the end of std::map<std::string, tEnumCouleur> xmap = boost::assign::map_list_of<std::string, tEnumCouleur>, asking for ";" 
        // static car ce get ne sappele pas sur un objet EnumCouleur (il sera toujours le même) cout<<EnumCouleurs::c_Str(v)
        static const char * c_Str(tEnumCouleur l) {
            return strEnumCouleur[l];}
        std::map<std::string, tEnumCouleur> xmap;
    private :
        static char * strEnumCouleur[];
        //EnumCouleur();

    };

and a .cpp to allow me to convert enum into string :

#include "EnumCouleur.h"
#include <string>

char * EnumCouleur::strEnumCouleur[] = {
    "BLACK","BLUE","RED","GREEN","YELLOW","CYAN"
};

i ve tried both things that i found on the topic i linkd :

std::map<std::string, tEnumCouleur> xmap = boost::map_list_of("A", A)("B", B)("C",C);

struct responseHeaderMap : public std::map<std::string, tEnumCouleur>
{
    responseHeaderMap()
    {
        this->operator[]("BLACK") =  BLACK;
        this->operator[]("BLUE") = BLUE;
        this->operator[]("RED") = RED;
        this->operator[]("GREEN") =  GREEN;
        this->operator[]("YELLOW") = YELLOW;
        this->operator[]("CYAN") = CYAN;
    };
    ~responseHeaderMap(){}
};

I really don't know how to use it.. let's say my program got a string from a textdocument. i'm sure that this string is correct. I want to make as an tEnumCouleur, in way to fit a contructor :

Segment( const Point p1, const Point p2, EnumCouleur::tEnumCouleur v);

How do i do that please ?

Community
  • 1
  • 1
Niko
  • 35
  • 7

1 Answers1

1

What you actually need to do is flip your std::map around so it maps string to enum rather than enum to string. Then, you can do xmap[string] and get your enum.

So you can do

std::map<std::string, tEnumCouleur> xmap = boost::assign::map_list_of<std::string, tEnumCouleur>(A, "A")(B, "B")(C, "C");

and then you can simply do xmap["BLACK"] and you'll get the enum value BLACK

Daniel
  • 6,595
  • 9
  • 38
  • 70
  • Also, it was a complete coincidence that I happened to open SO at the exact moment that you posted a question based off of my question. – Daniel Nov 20 '14 at 20:20
  • I don't really understand what you mean by "flip" my map sorry .. , so it's std::map = ... but where do i need to put it ? – Niko Nov 20 '14 at 20:24
  • " and you'll get the string "BLACK"" , you mean the enum right ? because what i want is the enum! – Niko Nov 20 '14 at 20:25
  • i just still don't know where to put the map declaration + value. seems like i can't do it below my enum declaration how to fix that ? – Niko Nov 20 '14 at 20:28
  • @Niko You will _have_ to put it below your enum declaration. It is impossible to answer you without providing more details such as error messages. – Daniel Nov 20 '14 at 20:30
  • 1
    @Niko "Check it please" is not a good way to get someone to help you. If you're not going to put any effort into it, why should I? – Daniel Nov 20 '14 at 20:39
  • well, i m not english native, this sounded polite to me. May you check it sir ? :) – Niko Nov 20 '14 at 20:40