C++中调用resize()函数时出现“No instance of overloaded function”错误的解决方法咨询
Fixing the "No instance of overloaded function" Error with
std::vector::resize Hey there! Let's break down what's going wrong with your code and get that IDFT function working properly.
Why You're Seeing That Error
The line causing the overload mismatch is this one:
x.resize(Xf.size(), static_cast<std::vector<float>>(0, 0));
Here's the issue:
- The
std::vector::resizemethod's second parameter expects an element of the vector's type (in your case, afloat), not anotherstd::vector<float>. - Your
static_castattempt is invalid syntax for creating a vector anyway—you don't need to construct a vector here at all.
The Correct Way to Resize and Initialize
To resize x to match the size of Xf and set all elements to 0, you just need to pass the desired size and a float value of 0. Either of these will work:
// Explicitly set initial value to 0.0f x.resize(Xf.size(), 0.0f); // Or omit the second parameter—float elements default to 0 x.resize(Xf.size());
Fixing Other IDFT Logic Bugs
While we're at it, there are a few logic issues in your loops that will prevent the IDFT from calculating correctly:
- You're using
x[k]instead ofXf[k]: Sincexstarts at 0, multiplying it by the exponential term will always leave it at 0. You need to use the input values fromXf. - Wrong exponential sign: Standard IDFT uses a negative exponent (opposite of DFT).
- Missing normalization: IDFT requires dividing the final result by the vector size to get the correct amplitude.
Here's the corrected full function:
#include <vector> #include <complex> #include <cmath> // Define PI if it's not already available #ifndef PI #define PI 3.14159265358979323846 #endif void IDFT(const std::vector<std::complex<float>> &Xf, std::vector<float> &x) { // Resize x to match Xf's size and initialize to 0 x.resize(Xf.size(), 0.0f); const size_t N = Xf.size(); for (size_t k = 0; k < N; ++k) { for (size_t m = 0; m < N; ++m) { // Negative exponent for IDFT calculation std::complex<float> expval(0, -2 * PI * static_cast<float>(k * m) / N); // Accumulate the real part of Xf[k] * exp into x[m] x[m] += std::real(Xf[k] * std::exp(expval)); } } // Normalize the result by the vector size for (auto &val : x) { val /= N; } }
Quick Recap
- Use
x.resize(Xf.size(), 0.0f)(or the shorter version without the second parameter) to fix the overload error. - Adjust the IDFT logic to use input values from
Xf, a negative exponent, and normalize the final result.
内容的提问来源于stack exchange,提问作者LYM
相关产品推荐
相关产品推荐

