带有嵌套模板的C++typedef不是类、结构或联合类型
C++ typedef with nested templates is not a class, struct, or union type
我不确定为什么以下代码没有用g++编译:
t.cpp: In instantiation of ‘Distrib<double>’:
t.cpp:28:56: instantiated from ‘Sampler<Distrib<Solution<double> > >’
t.cpp:35:48: instantiated from here
t.cpp:16:45: erreur: ‘double’ is not a class, struct, or union type
t.cpp:18:43: erreur: ‘double’ is not a class, struct, or union type
我希望能够在嵌套模板中传播AtomType
类型…
#include <iostream>
#include <vector>
template<typename T>
class Solution
{
public:
typedef T AtomType;
};
template<typename SOLT>
class Distrib
{
public:
typedef typename SOLT::AtomType AtomType;
typedef std::vector<AtomType> Matrix;
Matrix matrix;
};
template<typename DT>
class Sampler
{
public:
typedef typename DT::AtomType AtomType;
typedef typename Distrib<AtomType>::Matrix Matrix;
Matrix matrix;
};
int main()
{
Sampler< Distrib< Solution<double> > > sampler;
}
在Distrib
模板中,您有以下typedef
typedef typename SOLT::AtomType AtomType;
这意味着作为模板参数传入的任何类型都必须有一个AtomType
作为成员,而double
没有这样的东西。
如果你上了这样的课
class Double
{
typedef myType AtomType;
};
并将其作为模板参数传递给Distrib
模板,它将进行编译,因为Double::AtomType
确实存在。
在Sampler
类中,您有:
typedef typename Distrib<AtomType>::Matrix Matrix;
这里,AtomType
是double
,所以这是
typedef typename Distrib<double>::Matrix Matrix;
然后在你的Distrib
类中,行
typedef typename SOLT::AtomType AtomType;
扩展到
typedef typename double::AtomType AtomType;
因此出现了错误消息。我想你希望Sampler
类中的行是:
typedef typename DT::Matrix Matrix;
Distrib
类中的Matrix
typedef使用AtomType
,但我们期望的是DT
:
typedef typename Distrib<DT>::Matrix Matrix;
编译器看到double
在嵌套模板之间传播。
Distrib
是在Solution
的类型上模板化的;但在Sampler::Matrix
的定义中,您使用AtomType
作为模板参数。据推测,您只需要提供的Distrib
的类型与Sampler
:相同
template<typename DT>
class Sampler
{
// ...
typedef typename DT::Matrix Matrix;
};
相关文章:
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- <badref> 当使用 CallInst::CreateMalloc 作为结构类型时
- 给定一个C++嵌套的私有结构类型,是否有从文件范围静态函数访问它的策略
- 为什么"具有常量成员的结构"类型的指针不能指向"具有非常量成员的结构"?
- 查找声明为结构类型 C++ 的数组末尾
- C++ - 将结构类型的数组传递给用户定义的函数
- PowerShell 使用结构类型参数调用 C++ DLL 的导出函数
- NTTP 的结构类型:为什么所有成员都必须是公共的?
- 当我尝试访问结构类型时,我收到 std::bad_variant_access 异常
- 承诺可以使用结构类型吗?
- 填充模板化结构类型的结构成员
- 如何为结构类型变量赋值
- 在 c++ 中将结构类型转换为无符号字符*
- 在 c++ 中将一种结构类型分配给另一种类型
- 如何访问在 c++ 中在类内声明的结构类型指针变量?
- 确定输入"conscalc: calc"的类型时出错。不能联合编码器。具有不同字段集的结构类型
- 无法将结构类型的标识符设置为 nullptr;
- 结构类型成员的默认构造函数中的默认参数
- 如何在执行期间终止(或返回)具有结构类型的自动函数
- 在 stl 中迭代结构类型的队列