友模板类/模板非类型参数
C++: friend template class / template non-type parameter
本文关键字:类型参数 更新时间:2023-10-16
我想实现通用图形类,我仍然有问题,缩小到以下代码:
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;
我的想法是绝对错误的,我错过了什么,或者这样的结构是不可能的,混乱的,奇怪的,或者是一个非常(…)非常可怕的事情?
问题是您的friend
声明。我建议您先声明A
类,然后使用更简单的friend
声明。像
template<class T> class A;
template<class T>
class B
{
friend A<T>;
...
};
问题是您是否想让A<T>
成为B<T>
的朋友。那就用约阿希姆的溶液吧!
如果你想让任何A成为朋友,那么你需要这样做:
template <class T> class B
{
template<class> friend class A;
};
相关文章:
- 在 c++ 中的模板实例化中使用带有构造函数的类作为类型参数
- 如何解决一元"*"(有"字符")错误的无效类型参数?
- "std::shared_ptr":不是参数"_Ty"的有效模板类型参数
- 具有可变参数非类型参数的模板专用化
- 函数类型参数的模板参数推导
- PowerShell 使用结构类型参数调用 C++ DLL 的导出函数
- 对于非常量指针类型的参数,未调用具有常量指针模板类型参数的功能
- 为模板传递非类型参数 agument
- 为什么带有类型参数的运算符 () 可以应用于 result_of 上下文中的类型?
- 使用其他模板类型参数作为要在函数签名中使用的类型别名声明
- 如何避免具有相同类型参数的函数中的错误
- 将内置类型变量传递给只有一个类类型参数的"+"运算符函数时自动类型转换的构造函数
- c++非类型参数包扩展
- 如何实现对参数顺序不可知的std::same_as的广义形式(即对于两个以上的类型参数)
- 在不同的模板参数包之间分发非类型参数包
- 如何在使用容器和字符串时强制使用显式分配器类型参数
- 错误:一元"*"的类型参数无效(具有"int"):使用 mergesort 计算
- EXPECT_CALL具有 unique_ptr 引用类型参数的模拟函数
- 作为模板类型参数,为什么 type[N] 与其专用版本不匹配----模板<类 T>类 S<T[]>
- C++ 模板:重载时找不到基类类型参数方法