C++类 在"}"之前不允许使用函数定义

C++ Classes a function-definition is not allowed here before "}"

本文关键字:函数 定义 不允许 C++      更新时间:2023-10-16

我一直在尝试在linux上使用wxwidgets进行FFT,但我对C++不是很熟悉。我尝试了两种方法,但都没有任何运气,我一直在阅读所有关于寻找类似问题的错误,但我仍然不明白哪里出了问题。

第一种方法(类内所有内容)

#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; 

尝试编译时出错:

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

第二种方法-从方法中分离声明:

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 ===|

我的问题是:我正在搅乱的概念是什么?处理这一问题的正确方法是什么

编辑:其他问题->这行是什么?(IDE在创建类时插入了它)

标准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();

是该类的析构函数的声明,该函数用于在删除该类的实例时释放所需的所有内容。

  1. void fft 后有无效代码

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

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