调整大小时没有重载函数的实例

问题描述

我在使用 resize() 时收到“没有重载函数实例”的错误消息。我知道 resize() 里面的内容也可能是错误的。我正在尝试将 x,一个浮点数组调整为全零。不确定 std::vector>(0,0) 是否是正确的语法,因为我对 C++ 非常陌生。如何解决现有错误

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);
        }
    }
}

解决方法

这个:

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

应该

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

还有这个:

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

应该是:

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

a_function_that_converts_a_complex_float_to_a_float 的实现留作练习。您可以使用 complex<float> 的成员函数 real()imag()