Pouvez-vous m'aider, j'ai essayé de utiliser ce code mais je trouve quelque de problème au niveau de compilation précisément dans ce ligne SMARTENUM_DEFINE_GET_VALUE_FROM_STRING(Animal, animalName); . voila le code et merci.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cstring>
#include <string>
 
#define SMARTENUM_VALUE(typeName, value) e##typeName##_##value, 
#define SMARTENUM_STRING(typeName, value) #value, 
 
#define SMARTENUM_DEFINE_ENUM(typeName, values) enum typeName { values(SMARTENUM_VALUE) e##typeName##_Count };
 
#define SMARTENUM_DEFINE_NAMES(typeName, values) const char* typeName##Array [] = { values(SMARTENUM_STRING) };
 
#define SMARTENUM_DEFINE_GET_VALUE_FROM_STRING(typeName, name)	\
typeName get##typeName##FromString(const char* str) \
{	\
for (int i = 0; i < e##typeName##_Count; ++i) { \
if (!strcmp(##typeName##Array[i], str))	{	\
return (##typeName##)i;	}	\
return e##typeName##_Count;	}	\
}
 
#define getStringFromEnumValue(typeName, value) typeName##Array[##value]
#define getEnumValueFromString(typeName, name)	get##typeName##FromString(##name)
 
 
#define ANIMAL_LIST(m) \
m(Animal, Dog) \
m(Animal, Cat) \
m(Animal, Cow)
 
#define CAR_LIST(m) \
m(Car, Fiat) \
m(Car, Ford) \
m(Car, Audi)
int main()
{
 
 
SMARTENUM_DEFINE_ENUM(Animal, ANIMAL_LIST);
SMARTENUM_DEFINE_NAMES(Animal, ANIMAL_LIST);
const char* animalName = getStringFromEnumValue(Animal, eAnimal_Cow);
std::cout << animalName << std::endl;
SMARTENUM_DEFINE_GET_VALUE_FROM_STRING(Animal, animalName);
Animal animal = getEnumValueFromString(Animal, animalName);
system("pause");
return 0;
}