c++ - 从另一个模板结构继承的模板结构

标签 c++ templates c++03

我希望我的模板派生类 der 继承自 foo。 这只是一个测试代码,我想看看我是否在这里采用了正确的方法 der 类本身是模板化的,我要做的是在初始化列表 der 类中传递其基类 foo 的模板类型。我正在使用以下代码

template<typename t , typename u>
struct foo
{
  void foo_method(t inp , u out)
  {
        std::cout << inp + out << "\n";
  }
};


template <typename m>
struct der : public foo<t,u> //Error t was not declared in the correct scope
{
  der():foo<t,u>(int,int)//I want to specify the types for the base class in initialization list
  {

  }
};


int main()
{
   der<std::string> d;

}

现在,当我尝试运行上面的代码时,我得到了

Error t was not declared in the correct scope

关于如何解决这个问题有什么建议吗?

最佳答案

我想这就是您要找的:

template <typename m>
struct der : public foo<int,int>
{
  der():foo<int,int>()
  {

  }
};

关于c++ - 从另一个模板结构继承的模板结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27309094/

相关文章:

c++ - 为什么在 C++ 中使用 = 来初始化原始类型?

c++ - 为什么尝试在类中设置静态变量时出现链接器错误?

java - 为什么我们使用 if,else if 而不是多个 if block 如果主体是返回语句

c++ - 映射和设置插入功能无法解析

c++ - 在 C++ 中获取 <pair<int,int>, int*> 形式的映射的所有键

C++模板模板参数类型推导

c++ - 检查(原始)类型在 C++ 中是否可转换

c++ - 通过 C++ 模板访问不同的数据

c++ - 转换问题

c++ - 如何创建模板依赖成员类型