如何在结构中声明类模板并在以后初始化
How to declare a class template in a structure and initialize later?
我正在尝试对公差堆叠进行建模。我做了一个结构Layer
,它保持公差范围的下限(tol[0]
(和上限(tol[1]
(。我想在 tol[0]
和 tol[1]
之间生成一个随机值并将其分配给val
.
我的实现在结构中声明了uniform_real_distribution
类模板并在main()
中对其进行初始化,但是我在编译过程中遇到错误,使我认为我不能以这种方式使用类模板。
#include <random>
struct Layer {
double tol[2];
double val;
std::string name;
std::uniform_real_distribution<double> distribution;
};
int main()
{
Layer block;
block.tol[0] = .240;
block.tol[1] = .260;
std::default_random_engine generator;
block.distribution(block.tol[0],block.tol[1]);
block.val = block.distribution(generator);
return 0;
}
我从 g++ 收到以下错误:
error: no match for call to '(std::uniform_real_distribution<double>) (double&, double&)'
block.distribution(block.tol[0],block.tol1[]);
^
我创建了很多Layer
结构,所以我希望将发行版与结构相关联,但我不确定它是否可能。
在此阶段,对象已经构造完成,因此您可以执行以下操作:
block.distribution = std::uniform_real_distribution<double>(block.tol[0],block.tol[1]);
您也可以直接初始化结构:
Layer block{{.240,.260}, 0, "", std::uniform_real_distribution<double>(.240, .260)};
相关文章:
- 在C和C++中初始化结构中的数组
- 我们可以用参数化构造函数初始化结构的数组吗?
- 尝试初始化结构内的联合时出错
- 使用其他成员初始化结构的成员?
- 使用构造函数初始化结构还是在之后设置其值更好?
- 如果不初始化结构中的向量,它会自动为空还是具有随机内存位置的值?
- 如何在另一个结构中初始化结构数组?
- 如何在构造函数中初始化结构体的动态数组?
- 初始化结构中的联合 - c++
- 尝试初始化结构向量时出现编译错误
- 用于初始化结构的 void 指针强制转换
- 初始化结构的两种方式
- 使用用户定义的构造函数初始化C++结构
- 使用初始值设定项列表初始化结构,对于 std::array 失败
- 在 c++ 中初始化结构的指针
- 无序列图会创建一个零初始化结构吗?
- 如何在C++中初始化结构
- C 用结构指针初始化结构
- 在C 中初始化结构数组
- 使用数组聚合初始化结构数组