C++ boost函数的奇异行为
如果注释掉构造函数中的第一行,我下面的代码就会出错。返回错误为:C++ boost函数的奇异行为,c++,boost,eigen,C++,Boost,Eigen,如果注释掉构造函数中的第一行,我下面的代码就会出错。返回错误为: libc++abi.dylib: terminating with uncaught exception of type boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::overflow_error> >: Error in function boost::math::cyl_bess
libc++abi.dylib: terminating with uncaught exception of type boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::overflow_error> >: Error in function boost::math::cyl_bessel_k<double>(double,double): numeric overflow
Abort trap: 6
libc++abi.dylib:以boost::exception\u detail::clone\u impl:函数boost::math::cyl\u bessel\u k(double,double)中的错误类型的未捕获异常终止:数值溢出
中止陷阱:6
然而,奇怪的是,如果我在构造函数中输出了一些东西(例如,取消注释第一行),那么我的程序就可以正常工作
GP::GP(const Training_set& _sample, Eigen::VectorXd& param,
const std::string& which_kernel) : sample(_sample)
{
// std::cout << "WORKS" << std::endl;
if (which_kernel == "Matern") {
std::cout << "Matern kernel is used!" << std::endl;
Kernel_Matern matern(param, param.size());
kernel = &matern;
} else if (which_kernel == "Square_Exponential") {
std::cout << "Square Exponential kernel is used!" << std::endl;
Kernel_SE se(param, param.size());
kernel = &se;
} else {
std::cout << "Cannot identify the kernel" << std::endl;
exit(1);
}
input_dim = sample.dim;
input_size = sample.size;
L_chol.resize(sample.size, sample.size);
Eigen::MatrixXd cov_matrix = Eigen::MatrixXd::Zero(sample.size, sample.size);
get_cov_matrix(sample.X, input_size, sample.X, input_size, cov_matrix);
get_chol(cov_matrix);
}
GP::GP(常量训练集和样本,特征::向量xd和参数,
const std::string&哪个内核):示例(\u示例)
{
//std::cout您正在存储超出作用域的临时文件的地址。在指向超出作用域的内容之后使用*kernel
是未定义的行为
kernel
应为std::unique\u ptr
类型,而不是X*
类型
将分配替换为:
kernel = std::make_unique<Kernel_Matern>(param, param.size());
kernel=std::使_唯一(param,param.size());
或:
kernel=std::使_唯一(param,param.size());
在讨论的两行
如果您有将内核
传递给函数的代码,请改为传递kernel.get()
请注意,is会阻止复制GP
的实例,但不会移动它们,因为unique ptr是move only。如果您的类型将值和指针都存储到自己的值中,那么复制它可能是一个错误。您将超出范围的临时变量的地址分配给内核
内核
是一个悬而未决的问题比它所指向的数据更有效的指针。@VTT你说得对!!!有什么好方法来纠正这个问题?“内核”是类GP中的一个私有类。我想将“内核”初始化为“Matern”或“Square_”取决于用户的选择。@弗兰克,谢谢,这是错误的。但是在这种情况下如何正确使用多态性?kernel=std::make_unique(param,param.size());
kernel = std::make_unique<Kernel_SE>(param, param.size());