【问题标题】:Template Interface Implementation Class C++ error模板接口实现类 C++ 错误
【发布时间】:2013-09-24 02:29:56
【问题描述】:

我在模板继承方面遇到了一个小问题。

如果我用模板创建接口类:

template<typename Data>   
class InterfaceClass {
   private:
   public:
      virtual Data* foo() = 0; //some function that returns our template type
}

然后我创建一个实现:

template<typename MoData>
class Implementation : public InterfaceClass<MoData> {
   private:
   public:
      MoData* foo() { MoData* ptr = NULL; return ptr; } //some implementation
}

我似乎在我的编译器中遇到了这个问题。这不合法吗?

【问题讨论】:

    标签: c++ visual-studio-2010 oop interface


    【解决方案1】:
    template<typedef Data>
    

    不正确。

    你应该使用

    template<class Data>
    

    template<typename Data> 
    

    【讨论】:

    • 是的,哎呀,我的代码中有 typename ......不是 typedef ......我的坏......漫长的一天大声笑
    • @LeeJacobs 如果您添加缺少的;,我可以很好地编译:ideone.com/8PeBDB
    • @LeeJacobs 对不起,我在玩它,再试一次 :)
    • 好的,我在我的系统上测试了它,我知道我做错了什么。谢谢大佬。
    【解决方案2】:
    template <typedef Data>
    

    错了,用

    template <typename Data>   
    

    【讨论】:

      【解决方案3】:

      请在类声明的末尾添加分号。

      template<typename Data>   
      class InterfaceClass {
         private:
         public:
            virtual Data* foo() = 0; //some function that returns our template type
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-07
        • 1970-01-01
        • 1970-01-01
        • 2018-01-29
        • 1970-01-01
        • 1970-01-01
        • 2014-04-20
        相关资源
        最近更新 更多