C++类 在"}"之前不允许使用函数定义
C++ Classes a function-definition is not allowed here before "}"
我一直在尝试在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();
是该类的析构函数的声明,该函数用于在删除该类的实例时释放所需的所有内容。
-
void fft
后有无效代码 -
setDados
在类定义中具有void
返回类型,但它在您的cpp文件中返回int
,而您没有提供该文件。 -
virtual ~do_fft()
是一个虚拟析构函数。如果您将从这个类派生,建议使用它。如果没有,那就没有必要了。
相关文章:
- 不同翻译单元中不可重载的非内联函数定义
- Visual Studio中的函数声明和函数定义问题
- 编写代码时C++出现错误:错误 1 错误 C2601:'circle':本地函数定义是非法的
- 具有enable_if外部类原型的模板类构造函数定义
- 类的前向声明之后的类成员函数定义,在类声明之前
- 为函数定义符号不明确的指针参数
- C++模板专用化 - 无法匹配函数定义
- 错误:在第 6 行'{'标记之前,此处不允许使用函数定义
- 找不到 #define 的函数定义
- 根据类型特征更改函数定义?
- 将抽象基类中的所有纯虚函数定义为 varaidaic 模板
- 命名空间更改函数定义
- "Type&"与C++函数定义中的"Type*"
- C++:为什么允许在另一个函数中声明函数,而不允许在函数定义中声明?
- 如何从 C++ 中的现有模板函数定义新函数
- 私有在函数定义/实现的返回值范围内是什么意思 (c++)?
- 越界成员函数定义是否需要一个完全限定的类名,直到全局范围
- 为什么c++允许成员函数定义中实例的私有成员访问
- Qt基类函数定义
- C++函数定义中参数列表后面额外一对括号的含义