我知道这个问题已经在这里已经回答:C++ string to enum 但我真的失去了如何使用它,请不要鲁莽;)字符串到枚举C++
我想我的字符串转换在tEnumCouleur .. 我:
#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();
};
和一个.cpp,让我枚举转换为字符串:
#include "EnumCouleur.h"
#include <string>
char * EnumCouleur::strEnumCouleur[] = {
"BLACK","BLUE","RED","GREEN","YELLOW","CYAN"
};
我已经试过这两个THI我在话题发现NGS我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(){}
};
我真的不知道如何使用它..让说我的节目得到了从textdocument的字符串。我确定这个字符串是正确的。我想使作为tEnumCouleur,在方式,以适应构造器:
Segment(const Point p1, const Point p2, EnumCouleur::tEnumCouleur v);
我如何做到这一点吗?
可能重复://计算器。 com/questions/7163069/c-string-to-enum) – Aleksandar 2014-11-20 20:27:11
你读过船长吗? – Niko 2014-11-20 20:30:27
@尼科它是这个问题的重复。你不明白答案的事实并没有改变这一点。 – Daniel 2014-11-20 20:31:55