Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;与其他模板方法的模板类继承_C++_Templates - Fatal编程技术网

C++ c++;与其他模板方法的模板类继承

C++ c++;与其他模板方法的模板类继承,c++,templates,C++,Templates,我正在努力解决一个编译问题。给定一个以T为模板的基类,其中包含一个以U为模板的方法,我无法从派生类调用该方法。以下示例再现了我的问题: #包括 #包括 模板 阶级基础 { 受保护的: 模板 std::vector baseMethod()常量 { 返回std::vector(42); } }; 模板 派生类:公共基 { 公众: std::vector derivedMethod()常量 { 返回baseMethod(); } }; int main() { 导出d; std::vector re

我正在努力解决一个编译问题。给定一个以T为模板的基类,其中包含一个以U为模板的方法,我无法从派生类调用该方法。以下示例再现了我的问题:

#包括
#包括
模板
阶级基础
{
受保护的:
模板
std::vector baseMethod()常量
{
返回std::vector(42);
}
};
模板
派生类:公共基
{
公众:
std::vector derivedMethod()常量
{
返回baseMethod();
}
};
int main()
{
导出d;
std::vector res=d.derivedMethod();
返回0;
}
编译器结果:

t.cc:21:12: error: ‘baseMethod’ was not declared in this scope
t.cc:21:23: error: expected primary-expression before ‘int’
t.cc:21:23: error: expected ‘;’ before ‘int’
t.cc:21:26: error: expected unqualified-id before ‘>’ token

您应该添加
模板
关键字以将
基本方法
视为名称:

std::vector derivedMethod()常量
{
返回此->模板baseMethod();
}
在模板定义中,除非使用消歧关键字
template
(或除非已将其确定为模板名称),否则不将不是当前实例化成员的从属名称视为模板名称

有关进一步详情:

std::vector<int> derivedMethod() const
{
  return this->template baseMethod<int>();
}