简体   繁体   English

访问typedef映射结构中的成员?

[英]Accessing members in a typedef map struct?

My question should be simple and I've spent that last few hours trying to get an answer to it by figuring it out on my own (and google-ing) answers and getting nowhere. 我的问题应该很简单,我花了最后几个小时试图通过自己(和谷歌搜索)的答案来解决问题,却一无所获。

I've got the following in my header file: 我的头文件中包含以下内容:

Window.hpp

typedef struct {
    SDL_Window* window = 0;
    const char* title = 0;
    int width;
    int height;
    int flags;
} m_windowStruct;

typedef std::map<std::string, m_windowStruct> m_windowMap;

m_windowMap windowMap; /* <-- Made this accessible */

and in my source file I've the following code: 在我的源文件中,我有以下代码:

Window.cpp

bool Window::createWindow(std::string id, const char* title, int width, int height, int flags) {
    m_windowMap* windowMap;
    m_windowStruct windowData;

    windowData.window = SDL_CreateWindow("insert window creation info here");

    if (!windowData.window) {
        windowData.title = title;
        windowData.width = width;
        windowData.height = height;
        windowData.flags = flags;
        windowMap->insert(m_windowMap::value_type(id, &windowData));
        SDL_LogInfo(INFO, "Window creation successful");
        return true;
    }
    else {
        return false;
    }
};

Which works all fine and dandy... The problem I'm having is accessing the mapped struct members after. 一切正常,丹迪...我遇到的问题是访问映射的struct成员之后。

For example if I want the instance of the primary window I'd want the following function: 例如,如果我想要主窗口的实例,则需要以下功能:

/* Returns the window instance from the mapped struct */
SDL_Window* Window::getWindow(std::string id) {
    m_windowMap::const_iterator keyValuePair = windowMap.find(id); /* <-- New error: Error: expression must have a class type */

    if (keyValuePair == windowMap.end()) { /* <-- same error as just above */
        return 0;
    }

    return keyValuePair->second.window; /* <-- Same error as just above */
};

I cannot for the life of me figure out how to access the second window member so that I can then give it to my renderer! 我一辈子都无法弄清楚如何访问第二个window成员,然后再将其提供给渲染器!

Any ideas? 有任何想法吗?

I guess you want the windowMap being a global variable which is accessible across multiple source files (ie, .cpp). 我想您希望windowMap是一个全局变量,可以在多个源文件(即.cpp)之间进行访问。 Here are the changes you need to make, 这是您需要进行的更改,

In header file, replace the last line with extern m_windowMap* windowMap; 在头文件中,用extern m_windowMap* windowMap;替换最后一行extern m_windowMap* windowMap; . This makes variable accessible to all source files that include the header. 这使变量可被所有包含标头的源文件访问。

In your main function (or anywhere appropriate), add this line to initialize the map windowMap = new m_windowMap(); 在您的主函数(或任何适当的位置)中,添加以下行以初始化地图windowMap = new m_windowMap();

Remove m_windowMap* windowMap; 删除m_windowMap* windowMap; in the Window::createWindow function, otherwise the re-declaration creates a new local m_windowMap and the rest of the function makes no effect to the global windowMap . Window::createWindow函数中,否则重新声明将创建一个新的本地m_windowMap ,其余函数对全局windowMap

Lastly, since the global windowMap is a pointer, change .find() and .end() to ->find() and ->end() in the getWindow function. 最后,由于全局windowMap是一个指针, .find()getWindow函数.find().end()更改为->find()->end()

If the windowMap is indeed a class member of Window , it is actually easier and cleaner. 如果windowMap确实是Window的类成员,那么它实际上会更容易windowMap干净。

From the code you originally posted, remove m_windowMap* windowMap; 从您最初发布的代码中,删除m_windowMap* windowMap; in the Window::createWindow function. Window::createWindow函数中。 Otherwise the insert goes to this newly created local map. 否则, insert转到此新创建的本地地图。 Also, change ->insert to .insert as you did not declare windowMap as an pointer in the header. 另外,将->insert更改为.insert因为您没有在标题windowMap声明为指针。

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

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