【发布时间】:2014-12-05 05:00:30
【问题描述】:
我希望我的模板派生类 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
关于如何解决此问题的任何建议?
【问题讨论】:
-
如果你不提供模板参数,编译器应该如何知道你想从
foo<>继承什么? -
你的问题没有多大意义。
foo没有两个参数的构造函数,那么您要传递给它什么?t和u应该是什么?