0

好的,我以为我已经找到了模板类的实现文件,但显然没有......我在 VS 2013 C++ 解决方案中有以下文件:

主文件

#include "StateManager.h"
#include "State.h"

enum class Derp {
    Herp,
    Lerp,
    Sherp,
};

int main() {
    Game2D::State<Derp>::Context context(5);
    Game2D::StateManager<Derp> mgr(context);

    return 0;
}

状态管理器.h

#pragma once

#include "State.h"

namespace Game2D {

    template<typename Id>
    class StateManager {
    private:
        typename State<Id>::Context _context;

    public:
        explicit StateManager(typename State<Id>::Context context);
    };

#include "StateManager.inl"

}

状态管理器.inl

template<typename Id>
StateManager<Id>::StateManager(typename State<Id>::Context context) :
    _context(context)
{ }

状态.h

#pragma once

namespace Game2D {

    template<typename Id>
    class StateManager;

    template<typename Id>
    class State {
    public:
        struct Context {
            Context(int);
            int data;
        };

    private:
        StateManager<Id>* _manager;
        Context _context;

    public:
        State(StateManager<Id>&, Context);
        virtual ~State();

    };

#include "State.inl"

}

状态.inl

template<typename Id>
State<Id>::Context::Context(int data) {
    this->data = data;
}

template<typename Id>
State<Id>::State(StateManager<Id>& manager, Context context) :
    _manager(&manager),
    _context(context)
{ }
template<typename Id>
State<Id>::~State() { }

构建此项目会产生以下错误:

错误 10 错误 C1903:无法从先前的错误中恢复;停止编译 state.inl 9 1

错误 9 错误 C2065:“上下文”:未声明的标识符 state.inl 8 1

错误 7 错误 C2065:“经理”:未声明的标识符 state.inl 7 1

错误 8 错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int state.inl 7 1

错误 6 error C2039: 'State' : is not a member of '`global namespace'' state.inl 6 1

错误 1 ​​错误 C2143:语法错误:缺少 ';' 在'<'之前 state.inl 2 1

错误 2 错误 C2988:无法识别的模板声明/定义 state.inl 2 1

错误 3 错误 C2059:语法错误:'<' state.inl 2 1

错误 4 错误 C3083: 'Context': '::' 左侧的符号必须是 state.inl 2 1 类型

错误 5 error C2039: 'Context' : is not a member of '`global namespace'' state.inl 2 1

任何有关如何修复这些错误的帮助将不胜感激!

4

1 回答 1

0

一个疯狂的猜测是您将.inl文件作为独立的翻译单元添加到您的项目中,并且编译器试图将它们编译为独立的翻译单元。

这些文件作为独立的翻译单元毫无意义,它们不会这样编译。这些是包含文件(又名头文件)。它们应该被项目视为头文件。它们不应该直接编译。

于 2015-08-11T22:32:37.373 回答