No instance of overloaded function when resizing

谁说胖子不能爱 提交于 2021-02-05 09:19:13

问题


I got an error of "No instance of overloaded function" when I used resize(). I'm aware that what's inside of resize() might be wrong as well. I'm trying to resize x, an array of floats to all zeros. Not sure if std::vector>(0, 0) would be the right syntax for it as I am very new to C++. How do I solve the existing error?

void IDFT(const std::vector<std::complex<float>> &Xf, std::vector<float> &x)
{
    // allocate space for the vector holding the frequency bins
    // initialize all the values in the frequency vector to complex 0
    x.resize(Xf.size(), static_cast<std::vector<float>>(0, 0));
    // Xf.resize(x.size(), static_cast<std::complex<float>>(0, 0));
    // "auto" keyword is used for type deduction (or inference) in C++
    for (auto k = 0; k < Xf.size(); k++)
    {
        for (auto m = 0; m < x.size(); m++)
        {
            std::complex<float> expval(0, 2 * PI * (k * m) / x.size());
            x[k] += x[k] * std::exp(expval);
        }
    }
}

回答1:


This:

x.resize(Xf.size(), static_cast<std::vector<float>>(0, 0));

should be

x.resize(Xf.size());    // or x.resize(Xf.size(), 0.f); if you want to be explicit

and this:

x[k] += x[k] * std::exp(expval);

should be:

x[k] += a_function_that_converts_a_complex_float_to_a_float(x[k] * std::exp(expval));

The implementation of a_function_that_converts_a_complex_float_to_a_float is left as an exercise. You have the complex<float>'s member functions real() and imag() at your disposal.



来源:https://stackoverflow.com/questions/65968989/no-instance-of-overloaded-function-when-resizing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!