本文介绍了转换complex< int16_t>复杂到< double>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 11中仍然可以做到这一点:

Is there anyway in C++11 to do this:

 std::complex<int16_t> integer(42,42);
 std::complex<double> doub(25.5,25.5);
 std::complex<double> answer = integer*doub;

错误是

error: no match for ‘operator*’ (operand types are    
‘std::complex<short     int>’ and ‘std::complex<double>’)
std::complex<double> answer = integer*doub;

我尝试过static_cast;

I have tried static_cast like;

std::complex<double> answer = static_cast<std::complex<double>>(integer)*doub;


推荐答案

没有预定义的转换复杂< double> complex< int16_t> 或反之亦然。

您可以定义您自己的:

template <typename D, typename S> std::complex<D> cast(const std::complex<S> s)
{
    return std::complex<D>(s.real(), s.imag());
}

int main()
{
    std::complex<int16_t> integer(42, 42);
    std::complex<double> doub(25.5, 25.5);
    std::complex<double> answer = cast<double, int16_t>(integer)*doub;
}

这篇关于转换complex&lt; int16_t&gt;复杂到&lt; double&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 08:45