I am trying to create a function which does Fourier transformation but I am getting this error when I try to perform a dummy test. What am I doing wrong? I cannot seem to find out the error, I tried putting value in the array ‘a’ but it didn’t help. Any help will be greatly appreciated. Thank you

#define _USE_MATH_DEFINES #include <cmath> #include <math.h> #include <iostream> #include <array> class FV { public: float r, i, a; double p; int f; FV(float re, float im, float amp, double ph, int freq) { r = re; i = im; a = amp; p = ph; f = freq; } }; template <int SIZE> std::array<FV, SIZE> DiscreteFourierSeries(std::array<float, SIZE>& x) { const unsigned int size = x.size(); std::array<FV, size> X; for (int k = 0; k < size; ++k){ int re = 0; int im = 0; for (int n = 0; n < size; ++n) { const float phi = (2 * M_PI * k * n) / size; re += x[n] * cos(phi); im -= x[n] * sin(phi); } re = re / size; im = im / size; float amp = sqrt(re * re + im * im); double phase = atan2(static_cast<double> (im), static_cast<double> (re)); FV fourierVariables = FV(re, im, amp, phase, k); X[k] = fourierVariables; delete fourierVariables; } return X; } int main() { std::array<float, 8> a; std::array<FV, 8> k = DiscreteFourierSeries(a); return 0; }

## Answer

Before I answer your question, I would strongly encourage you to learn C++ a bit more before proceeding further. The reason I say this is because your code has very basic errors, and me fixing it for you will not really advance your knowledge in C++.

When I compile your code, I get the following error:

error: no matching function for call to ‘DiscreteFourierSeries(std::array<float, 8>&)’ 53 | std::array<FV, 8> k = DiscreteFourierSeries(a); .... ./x.cpp:53:50: note: mismatched types ‘int’ and ‘long unsigned int’

Note, the “note” at the end. It tells you the problem clearly: “mismatched types”. But that doesn’t tell us a lot. Where are these types being mismatched? Looking at the docs of `std::array`

, we see:

template< class T, std::size_t N > struct array;

But your function is:

template <int SIZE> std::array<type, SIZE> ... yourfunc(std::array<type, SIZE>)

This is where we can clearly see the “mismatched types”. You have an `int`

as the template parameter, but `std::array`

wants `size_t`

. Changing it to:

template <size_t SIZE> ...

fixes the error, but unfortunately introduces a ton more 🙂

Next you get the error:

./x.cpp:26:26: error: the value of ‘size’ is not usable in a constant expression 26 | std::array<FV, size> X;

This points to the following lines in your code:

const unsigned int size = x.size(); std::array<FV, size> X;

That looks okay, but is it? The error says, `the value of ‘size’ is not usable in a constant expression`

. What exactly is a constant expression? Looking at the docs again:

Defines an expression that can be evaluated at compile time.

It’s important to note here that the statement:

const unsigned int size = x.size();

is not the problem here, the usage of this variable `size`

is the problem. The value of `size`

is not available at compile time, so it can not be used to create an array. Hence the following line:

std::array<FV, size> X;

is incorrect. To fix this, simply use the `SIZE`

template parameter instead of `size`

.

Once done, the error disappears, but brings new kinds of errors 😉

./x.cpp:26:26: error: use of deleted function ‘std::array<FV, 8>::array()’ 26 | std::array<FV, SIZE> X; | ^ In file included from ./x.cpp:6: /usr/include/c++/10.2.0/array:94:12: note: ‘std::array<FV, 8>::array()’ is implicitly deleted because the default definition would be ill-formed: 94 | struct array | ^~~~~ /usr/include/c++/10.2.0/array:94:12: error: no matching function for call to ‘FV::FV()’

The last line tells us the problem: `error: no matching function for call to ‘FV::FV()’`

. We don’t have a default constructor in `class FV`

. Let’s create one:

class FV { public: .... FV() = default; };

Once fixed, one last error remains:

./x.cpp:47:16: error: type ‘class FV’ argument given to ‘delete’, expected pointer 47 | delete fourierVariables; | ^~~~~~~~~~~~~~~~

You are trying to delete a non pointer variable, this is invalid. Just remove the statement and your program will compile.

I would again suggest you to learn C++ a bit more, or at least learn the things and tools that you are using so that you can at least figure out when something breaks. Don’t use stuff that you don’t understand, it will only create problems for you.