-1

I've been trying to make a FFT with wxwidgets on linux, but I am not very familiar with C++. I've tried two approaches both without any lucky, and I've been reading all about the errors looking for similar problems and I still don't understand what's wrong.

First Approach (everything inside the class)

#include <valarray>
#include <complex>
#include <sstream>
#include <iterator>
#include <vector>

class do_fft
{
public:
    typedef std::complex<double> Complex;
    typedef std::valarray<Complex> CArray;
        do_fft();
        virtual ~do_fft();
private:
    const static double PI = 3.141592653589793238460;
    CArray x;
    void setDados(CArray v)
    {
        CArray x = v;
    }
    CArray getFFT()
    {

        void fft(CArray& x)
        {  //line 27
            const size_t N = x.size();
            if (N <= 1) return;

            // divide
            CArray par = x[std::slice(0, N/2, 2)];
            CArray  impar = x[std::slice(1, N/2, 2)];

            // conquistar
            fft(par);
            fft(impar);

            // combinar
            for (size_t k = 0; k < N/2; ++k)
            {
                Complex t = std::polar(1.0, -2 * PI * k / N) * impar[k];
                x[k    ] = par[k] + t;
                x[k+N/2] = par[k] - t;
            }
        }
        fft(x);
        return x;
    } //line 49
} fftd; 

Errors when trying to compile:

do_fft.h|49|error: expected ‘;’ after
class definition| do_fft.h||In member function ‘do_fft::CArray
do_fft::getFFT()’:| do_fft.h|27|error: a function-definition is not
allowed here before ‘{’ token| do_fft.h|49|error: expected ‘}’ at end
of input| do_fft.h|49|warning: no return statement in function
returning non-void

Second Approach - Separate declarations from methods:

class do_fft
{
public:
    typedef std::complex<double> Complex;
    typedef std::valarray<Complex> CArray;
    //    do_fft();
    //    virtual ~do_fft();
private:
    const static double PI = 3.141592653589793238460;
    CArray x;
    void setDados(CArray v);
    CArray getFFT();
} fftd;


do_fft.cpp|3|error: ISO C++ forbids declaration of ‘setDados’ with no type [-fpermissive]| 
do_fft.cpp|3|error: prototype for ‘int do_fft::setDados(do_fft::CArray)’ does not match any in class ‘do_fft’| 
do_fft.h|19|error: candidate is: void do_fft::setDados(do_fft::CArray)| do_fft.cpp|8|error: ‘getFFT’ in ‘class do_fft’ does not name a type| ||=== Build finished: 4 errors, 0 warnings ===|

My question is: what are the concepts that I am messing around and what would be the proper way to handle this ?

EDIT: Other question-> what does "virtual ~do_fft();" this line? (the IDE inserted it when creating the class)

4

2 回答 2

1
  1. 之后您的代码无效void fft

  2. setDados在类定义中有void返回类型,但它int在您未提供的 cpp 文件中返回。

  3. virtual ~do_fft()是一个虚拟析构函数。如果您将从此类派生,则建议使用。如果没有,那就没有必要了。

于 2013-08-28T02:32:27.357 回答
1

标准 C++ 中不允许嵌套函数,您可以这样做:

class do_fft
{
public:
    typedef std::complex<double> Complex;
    typedef std::valarray<Complex> CArray;
        do_fft();
        virtual ~do_fft();
private:
    const static double PI = 3.141592653589793238460;
    CArray x;
    void setDados(CArray v)
    {
        CArray x = v;
    }

    void fft(CArray& x)
    {  //line 27
       const size_t N = x.size();
       if (N <= 1) return;

       // divide
       CArray par = x[std::slice(0, N/2, 2)];
       CArray  impar = x[std::slice(1, N/2, 2)];

       // conquistar
       fft(par);
       fft(impar);

       // combinar
       for (size_t k = 0; k < N/2; ++k)
       {
           Complex t = std::polar(1.0, -2 * PI * k / N) * impar[k];
           x[k    ] = par[k] + t;
           x[k+N/2] = par[k] - t;
       }
    }


    CArray getFFT()
    {
        fft(x);
        return x;
    } //line 49
} fftd; 

另外,线

virtual ~do_fft();

是这个类的析构函数的声明,这个函数用于在这个类的实例被删除时释放所有你需要的东西。

于 2013-08-28T02:41:33.083 回答