Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.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++_Class_Templates_Friend_Non Type - Fatal编程技术网

C++ C++;:好友模板类/模板非类型参数

C++ C++;:好友模板类/模板非类型参数,c++,class,templates,friend,non-type,C++,Class,Templates,Friend,Non Type,我想实现通用图类,但仍然存在一些问题,这些问题可以归结为以下代码: template <class T> class B { template <T> friend class A; }; template <class T> class A { private: std::vector<B<T> *> myBs; }; class C { }; 模板类B { A类模板; }; 模板类别A { 私人: std:

我想实现通用图类,但仍然存在一些问题,这些问题可以归结为以下代码:

template <class T> class B
{
    template <T> friend class A;
};


template <class T> class A
{
private:
    std::vector<B<T> *> myBs;
};


class C { };
模板类B
{
A类模板;
};
模板类别A
{
私人:
std::载体myBs;
};
C类{};
除非我这样做,否则编译得相当好:

B<C> myB;
B-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;
B.h:在“类B”的实例化中:
在A.h:12:0中包含的文件中,
来自main.cpp:16:
main.cpp:30:10:从这里开始需要
B.h:15:1:错误:“C类”不是模板非类型参数的有效类型
{
^
B.h:11:11:错误:模板参数“T类”
甲级模板;
^
B.h:16:31:错误:此处重新声明为“”
A类模板;

我的想法是绝对错误的,我是否遗漏了一些东西,或者这样的构造是不可能的、混乱的、奇怪的或者是一件非常(…)非常可怕的事情呢?

问题是你的
朋友
声明。我建议你先声明
一个
类,然后使用一个更简单的
朋友
声明。比如

template<class T> class A;

template<class T>
class B
{
    friend A<T>;
    ...
};
A类模板;
模板
B类
{
朋友A;
...
};

问题在于您的
朋友
声明。我建议您先声明
A
类,然后使用更简单的
朋友
声明。如

template<class T> class A;

template<class T>
class B
{
    friend A<T>;
    ...
};
A类模板;
模板
B类
{
朋友A;
...
};

问题是你是否只想让
A
成为
B
的朋友。 然后使用约阿希姆的解决方案

如果你想交朋友,那么你需要这样做:

template <class T> class B
{
    template<class> friend class A;
};
模板类B
{
A类模板;
};

问题是你是否只想让
A
成为
B
的朋友。 然后使用约阿希姆的解决方案

如果你想交朋友,那么你需要这样做:

template <class T> class B
{
    template<class> friend class A;
};
模板类B
{
A类模板;
};

好主意,但目前我只想交一个B好主意的朋友,但目前我只想交一个B的朋友