C++ 在与父类具有相同类型的类中声明模板类
花式cout/shiftC++ 在与父类具有相同类型的类中声明模板类,c++,templates,namespaces,ostream,C++,Templates,Namespaces,Ostream,花式cout/shift操作员应该是: template <typename V, typename W> std::ostream &operator<< (std::ostream& os, typename Foo<V,W>::template Bar<V,W> &b) {return os;} 模板 std::ostream&operator通过在缺少的尾部添加一个到类栏定义?Foo::Bar有意义吗?或者你总是想
操作员应该是:
template <typename V, typename W>
std::ostream &operator<< (std::ostream& os, typename Foo<V,W>::template Bar<V,W> &b) {return os;}
模板
std::ostream&operator通过在缺少的尾部添加一个代码>到类栏
定义?Foo::Bar
有意义吗?或者你总是想要Foo::Bar
?实际上只有后一个会出现,但我想第一个会很好。Foo::Bar
应该是typename Foo::Bar
@Pradhan:你也错过了缺少的模板。第一个模板不起作用,你无法根据Bar
(这是传入的真实参数),并且Foo
本身处于非推断上下文第二个也不起作用,它也是非推断上下文context@PiotrS.:它可以工作,我们只需要显式地给出模板参数(我同意,这对用户不友好)。只是添加了版本以避免此问题。@drahnr“第一个代码块就是我想要的。”它可以编译,但是std::cout
error: expected template-id for type before ‘*’ token
std::ostream &operator<<(std::ostream &os, Foo<V, W>::template Bar<V, W> *b)
template <typename V, typename W>
std::ostream &operator<< (std::ostream& os, typename Foo<V,W>::template Bar<V,W> &b) {return os;}
template <typename X, typename Y> class Foo {
public:
class Bar {
X a;
Y b;
};
Bar baba;
};
template <typename V, typename W>
std::ostream &operator<< (std::ostream& os, const typename Foo<V,W>::Bar& b) {return os;}
template <typename X, typename Y> class Foo {
public:
class Bar {
X a;
Y b;
friend
std::ostream &operator<< (std::ostream& os, const Bar &b) {return os;}
};
Bar baba;
};