【发布时间】:2014-10-03 23:21:59
【问题描述】:
我想实现通用图形类,但我仍然遇到问题,缩小到以下代码:
template <class T> class B
{
template <T> friend class A;
};
template <class T> class A
{
private:
std::vector<B<T> *> myBs;
};
class C { };
除非我这样做,否则编译得很好:
B<C> myB;
...导致以下错误:
B.h: In instantiation of ‘class B<C>’:
In file included from A.h:12:0,
from main.cpp:16:
main.cpp:30:10: required from here
B.h:15:1: error: ‘class C’ is not a valid type for a template non-type parameter
{
^
B.h:11:11: error: template parameter ‘class T’
template <class T> class A;
^
B.h:16:31: error: redeclared here as ‘<declaration error>’
template <T> friend class A;
我的想法是绝对错误的吗,我是否遗漏了什么,或者这样的结构是不可能的、混乱的、奇怪的或非常(...)非常糟糕的事情?
【问题讨论】:
标签: c++ class templates friend non-type