C++使用模板定义结构

Tre*_*ton -1 c++ templates struct

我不明白为什么我不能定义这个结构:

//Class.h

template <class T>
struct Callback {
    T* Object;
    std::function<void()> Function;
};

template <class T>
struct KeyCodeStruct {
    typedef std::unordered_map<SDL_Keycode, Callback<T>> KeyCode;
};

template <class T>
struct BindingStruct{
    typedef std::unordered_map<int, KeyCodeStruct<T>> Binding;
};


class Class {
public:
    template <class T>
    void bindInput(SDL_EventType eventType, SDL_Keycode key, Callback<T> f);
private:
    template <class T>
    BindingStruct<T> inputBindings; //How do I define this? This gives me an error.
}
Run Code Online (Sandbox Code Playgroud)

它给出了错误: Member 'inputBindings' declared as a template. 我不太了解模板所以我可能只是错过了我需要的信息.

更新(回应deviantfan)

现在我的cpp类遇到了我所拥有的函数的问题.

template <class T>
void InputManager::bindInput(SDL_EventType eventType, SDL_Keycode key, Callback<T> f)
{
    inputBindings[eventType][key] = f;
}
Run Code Online (Sandbox Code Playgroud)

它说预期的类或命名空间.

dev*_*fan 6

错误:

class Class {
private:
    template <class T>
    BindingStruct<T> inputBindings;
}
Run Code Online (Sandbox Code Playgroud)

对:

template <class T>
class Class {
private:
    BindingStruct<T> inputBindings;
}
Run Code Online (Sandbox Code Playgroud)

  • @TrevorPeyton它回答了你问的问题.每次弹出新的编译器错误时,您是否会继续发布对问题的更改? (2认同)