【发布时间】:2018-11-08 05:05:08
【问题描述】:
如何使用“A”类的方法返回“B”类型?例如:
template <typename T> class A{
//something
template <typename V> class B{
//something
};
B& foo(){
B<T> y; //the variable must have the same type T of the father class (for my exercise)
//something
return y;
}
};
主要:
A <int> o;
o.foo();
一旦我尝试编译它,它就会给我这些错误:
“在“B& foo()...”中“在没有参数列表的情况下无效使用模板名称 'A::B'”
和
"'class A' 没有名为 'foo' 的成员"
我在关闭B类后写了函数“foo”,所以可能是对的……
【问题讨论】:
-
只是一个提示。在这里:
B& foo(){。这个错误很容易理解。 -
在返回
B时不能省略模板参数,只有在你愿意返回A<T>时才允许这样做(即A与foo声明的模板参数相同)。你应该返回B<T>,而不仅仅是B。
标签: c++ class templates nested