【问题标题】:Nested classes with Templates c++带有模板的嵌套类 c++
【发布时间】:2019-02-25 05:33:50
【问题描述】:

我正在尝试实现嵌套类中的方法,即模板内的方法,但是我无法做到这一点。我是 cpp 的新手,想知道我是否可以按照我在这里编码的方式来实现它,比如在命名空间和类范围之外。

namespace structures {
template<typename T>
class a {
   public:
      int funca();
      int funcb();
   private:
      class b {
        void funcc();
      }
}

template<typename T>
int a<T>::funca() {
    // method implementation
}

template<typename T>
int a<T>::funcb() {

  // method implementation

}

// HOW DO i IMPLEMENT THE class b METHODS HERE??

template<typename T>
void a<T>::b<T>::funcc(){  
   //Didn't work
}
};

【问题讨论】:

  • 函数需要返回类型。
  • a&lt;T&gt;::b 不是模板。它不需要模板参数。

标签: c++ class templates nested


【解决方案1】:

试试这个void a&lt;T&gt;::b::funcc():

template<typename T>
class a
{
public:
    int funca();
    int funcb();
private:
    class b
    {
        void funcc();
    };
};

template<typename T>
int a<T>::funca()
{
}

template<typename T>
int a<T>::funcb()
{
}

template<typename T>
void a<T>::b::funcc()
{
}

【讨论】:

    猜你喜欢
    • 2018-11-08
    • 2011-05-04
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 2018-04-22
    • 2018-02-06
    • 2023-03-29
    相关资源
    最近更新 更多