3

我一直在尝试使用模板化基类并从它继承来实现服务定位器模式:

    // Header File
    namespace one {
    template <class I, class N>
    class Locator {
    public:
        static void initialize() {
            service = &nullService;
        }
        static void provide(I* newService) {
            if (newService == 0) {
                initialize();
            } else {
                service = newService;

        }
        static I& get() { return *service; }
        virtual ~Locator() {
            if (service != &nullService) {
                delete service;
            }
    private:
        Locator();
        static I* service;
        static N nullService;
    };
    }

    // Source File
    #include "Locator.hpp"

    namespace one {
    template<class I, class N>
    I* Locator<I, N>::service;

    template<class I, class N>
    N Locator<I, N>::nullService;
    }

类 I是实例类,类 N是空服务类。对于派生类:

    // Header File
    namespace two {
    class ExampleLocator : public one::Locator<Service, NullService> {}
    }

    // Source File
    namespace one {
    template class Locator<Service, NullService>;
    }

当我尝试使用这个实现时,

    int main(int argc, char const *argv[]) {
        two::ExampleLocator::initialize();
        two::ExampleLocator::get().doSomething();
    }

gcc 编译失败,出现以下错误:

  • 对 one::Locator::initialize() 的未定义引用
  • 在函数 one::Locator::get(): undefined reference to one::Locator::service

我究竟做错了什么?

注意:我可以通过在派生类的 cpp 中重新声明定位器的静态属性来编译项目:

    // Alternative Derived class Source file
    namespace one {
    template<class I, class N>
    I* Locator<I, N>::service;

    template<class I, class N>
    N Locator<I, N>::nullService;

    template class Locator<Service, NullService>;
    }

注意 2:Derived 类与 Locator 类位于不同的命名空间中。

4

1 回答 1

5

模板定义必须在所有使用它们的翻译单元中可见。将 .cpp 文件中的代码移动到 .hpp 文件中。

参见:为什么模板只能在头文件中实现?

于 2012-12-19T12:19:38.953 回答