简体   繁体   English

C ++静态结构模板方法返回枚举类型

[英]C++ static struct template method to return enum type

I don't really know c++ but I have been messing around with it for some time now, although I know I should really take the time to learn it. 我不是很了解c ++,但是我已经花了一段时间了,尽管我知道我应该花些时间来学习它。

Anyway the problem I have here is with a static struct, I am uncertain if the structure of this is correct. 无论如何,我这里的问题是静态结构,我不确定它的结构是否正确。

enum ItemTypes_t {  
    ITEM_TYPE_KEY,
    ITEM_TYPE_MAGICFIELD,
    ITEM_TYPE_DEPOT,
    ITEM_TYPE_REWARDCHEST,
    ITEM_TYPE_MAILBOX,
    ITEM_TYPE_TRASHHOLDER,
    ITEM_TYPE_TELEPORT,
    ITEM_TYPE_DOOR,
    ITEM_TYPE_BED,
    ITEM_TYPE_RUNE
};

template <typename T>
static struct tTypes {
public:
    const char* name;
    T value;
    T getEnums(std::string itemName);
};

tTypes<ItemTypes_t>itemEnum[] = {
    { "key", ITEM_TYPE_KEY },
    { "magicfield", ITEM_TYPE_MAGICFIELD },
    { "depot", ITEM_TYPE_DEPOT },
    { "rewardchest", ITEM_TYPE_REWARDCHEST },
    { "mailbox", ITEM_TYPE_MAILBOX },
    { "trashholder", ITEM_TYPE_TRASHHOLDER },
    { "teleport", ITEM_TYPE_TELEPORT },
    { "door", ITEM_TYPE_DOOR },
    { "bed", ITEM_TYPE_BED },
    { "rune", ITEM_TYPE_RUNE }
};

ItemTypes_t tTypes<ItemTypes_t>::getEnums(std::string itemName) {
    for (int i = 0; itemEnum[i].name; i++) {
        if (itemEnum[i].name == find) {
            return itemEnum[i].value;
        }
    }
    return;
};

I create an instance of it like this. 我这样创建它的一个实例。

tTypes<ItemTypes_t> itemType = tTypes<ItemTypes_t>();

And then call it like this. 然后这样称呼它。

ItemTypes_t storage;

storage = itemType.getEnums("some item name is passed here as a string");

There are no errors but I really don't understand the structure or don't even know if it will work. 没有错误,但我真的不了解该结构,甚至不知道它是否可以工作。

Edit: Here are some errors 编辑:这是一些错误

error C2988: unrecognizable template declaration/definition
error C2059: syntax error: '<end Parse>'
error C2977: 'tTypes': too many template arguments
note: see declaration of 'tTypes'
error C2977: 'tTypes': too many template arguments
note: see declaration of 'tTypes'
error C2027: use of undefined type 'tTypes'
note: see declaration of 'tTypes'
error C2027: use of undefined type 'tTypes'
note: see declaration of 'tTypes'
error C2027: use of undefined type 'tTypes'
note: see declaration of 'tTypes'
error C2561: 'getEnums': function must return a value
note: see declaration of 'getEnums

I tried using a map but that didn't work 我尝试使用地图,但没有用

std::map<std::string, ItemTypes_t> tTypes = {
    { "key", ITEM_TYPE_KEY },
    { "magicfield", ITEM_TYPE_MAGICFIELD },
    { "depot", ITEM_TYPE_DEPOT },
    { "rewardchest", ITEM_TYPE_REWARDCHEST },
    { "mailbox", ITEM_TYPE_MAILBOX },
    { "trashholder", ITEM_TYPE_TRASHHOLDER },
    { "teleport", ITEM_TYPE_TELEPORT },
    { "door", ITEM_TYPE_DOOR },
    { "bed", ITEM_TYPE_BED },
    { "rune", ITEM_TYPE_RUNE },
};

Edit 2: 编辑2:

I used the c++ compiler online to test how I think it should work and it works fine (the map) but when I try it in the project it doesn't work at all. 我在线使用了c ++编译器来测试我认为它应该如何工作并且可以正常工作(地图),但是当我在项目中尝试时,它根本无法工作。

This is just a really basic example. 这只是一个非常基本的例子。

#include <iostream>
#include <map>

using namespace std;

enum a : unsigned int {
    RED = 23, BLACK = 43, BLUE = 56
};

map<string, a> colors = {
    {"black", BLACK},
    {"green", RED},
    {"yellow", BLUE}
};

int main()
{
   cout << "test map " << colors["green"] << endl; 

   return 0;
}

This is the results 这是结果

sh-4.3$ g++ -std=c++11 -o main *.cpp                                                                                                                                
sh-4.3$ main                                                                                                                                                        
test map 23 

Thanks everyone for the advice, the problem isn't the map or well the map works.. the monstrosity I tried to construct with the static struct template was obviously wrong.. 感谢大家的建议,问题不在于地图还是地图工作正常。.我试图用静态结构模板构造的怪兽显然是错误的。

The real problem here is where it is reading the strings from. 真正的问题是从哪里读取字符串。

I'll just explain, normally this data is read from an xml file but I constructed some code to generate a lua table from the parsing of the xml file so that I could re-write the function to load the lua table which had issues of nesting errors so this is why i am using a map, actually several. 我只是解释一下,通常这些数据是从xml文件中读取的,但是我构造了一些代码以通过xml文件的解析生成lua表,以便我可以重新编写函数以加载存在问题的lua表嵌套错误,所以这就是为什么我要使用地图,实际上是几个。

I've been working on this for hours, so its natural to over look something, the strings in the code are not problem nor are maps, its the lua file, some of its properties are different character case.. so naturally when I scan an index of the map even tho its the same spelled word its a different character case. 我已经工作了几个小时,所以可以自然地查看某些东西,代码中的字符串也没问题,映射也没有问题,它的lua文件,其某些属性是不同的字符大小写。地图的索引,即使是相同的拼写单词,也具有不同的字符大小写。

Solution: 解:

The easiest solution would be for me to lowercase the properties when I am constructing the lua table. 对于我来说,最简单的解决方案是在构建lua表时将属性小写。

Conclusion: 结论:

That worked, instead of literally 50k unread properties now I only am down to maybe 20 or 30 unread properties.. thanks everyone for help, I'll definitely check out those books! 那行得通,而不是真正的50k未读属性,现在我只剩下20或30个未读属性。.谢谢大家的帮助,我一定会看看那些书的!

You define yout getEnums function like this: 您可以这样定义您的getEnums函数:

ItemTypes_t tTypes<ItemTypes_t>::getEnums(std::string itemName) { ... }

However that's not how a member function of a templated class is defined. 但是,这不是定义模板类的成员函数的方式。 What this is, is a specialization and you can't specialize a single member of a templated structure, you must specialize the whole structure. 这是一种专门化 ,您不能专门化模板结构的单个成员,而必须专门化整个结构。

You should instead do 你应该做

template<typename T>
T tTypes<T>::getEnums(std::string itemName) { ... }

There are other things that you should look at. 您还应该注意其他事项。 Like the mapping between the strings and the enumeration values which should probably be passed to the tTypes class as an argument to the constructor and stored internally inside the class. 就像字符串和枚举值之间的映射一样,应该将其作为构造函数的参数传递给tTypes类,并内部存储在该类内部。

Your code can be implemented with std::map and using a singleton class as follows: 您的代码可以使用std :: map并使用singleton类来实现,如下所示:

#include <map>
#include <string>

enum class repo_t {ITEM_TYPE_MAGICFIELD,ITEM_TYPE_DEPOT}; //etc

class repo
{
   private:
     std::map<std::string,repo_t> items;
     repo()
     {
     items.insert(std::make_pair("magicfield", repo_t::ITEM_TYPE_MAGICFIELD));
     items.insert(std::make_pair("depot", repo_t::ITEM_TYPE_DEPOT ));
     items.insert(std::make_pair("rewardchest",repo_t::ITEM_TYPE_REWARDCHEST ));
         //etc
      } 

   public:

   static repo*& get_instance()
    {
        static repo* obj(new repo());
        return obj;
    }

    ~repo(){}

    bool find(std::string key, repo_t& result)
    {
       auto it = items.find(key);
       if(it ==  items.end())
             return false;
       result = *it;
       return true;
    }


};

//usage

void main()
{
    repo_t result;
    bool found = repo::get_instance()->find("magicfield",result);
    if(found)
    {
       // do something with result
    }
    else
    {
      // key not found
    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM