1

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);
        }
    }
}
linda meng
  • 53
  • 5
  • 4
    Please post code as text not as image. `x` is vector of float, so the second parameter of `resize` should be a float, not a vector. But fro the rest of your code it looks like `x` should be a vector of `std::complex` instead. – Lukas-T Jan 30 '21 at 14:21
  • 1
    If you look at the [documentation](https://en.cppreference.com/w/cpp/container/vector/resize) you'll see that the second parameter to `resize` should be (or be implicitly convertible to) the same type as each `vector` element -- `float` in this case. – G.M. Jan 30 '21 at 14:30
  • Reading your comments in the code I'm wondering what the resizing is all about. Do you really want to resize `Xf` to the size of `x`? That would be pointless since `x` was just resized to be the size of `Xf`. – Ted Lyngmo Jan 30 '21 at 15:06

1 Answers1

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.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108