如何解决没有匹配的函数调用‘DiscreteFourierSeries(std::array<float, 8>&)’
我正在尝试创建一个执行傅立叶变换的函数,但是当我尝试执行虚拟测试时出现此错误。我究竟做错了什么?我似乎无法找出错误,我尝试将值放入数组 'a' 中,但没有帮助。任何帮助将不胜感激。谢谢
#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;
}
解决方法
在我回答您的问题之前,我强烈建议您在继续之前多学习 C++。我这么说是因为你的代码有非常基本的错误,我为你修复它不会真正提高你的 C++ 知识。
当我编译您的代码时,出现以下错误:
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’
注意,末尾的“注意”。它清楚地告诉您问题:“类型不匹配”。但这并没有告诉我们很多。这些类型在哪里不匹配?查看 std::array
的文档,我们看到:
template<
class T,std::size_t N
> struct array;
但你的功能是:
template <int SIZE> std::array<type,SIZE> ... yourfunc(std::array<type,SIZE>)
在这里我们可以清楚地看到“不匹配的类型”。您有一个 int
作为模板参数,但 std::array
需要 size_t
。改为:
template <size_t SIZE> ...
修复了错误,但不幸的是引入了更多:)
接下来你会得到错误:
./x.cpp:26:26: error: the value of ‘size’ is not usable in a constant expression
26 | std::array<FV,size> X;
这指向您代码中的以下几行:
const unsigned int size = x.size();
std::array<FV,size> X;
看起来没问题,但是是吗?错误说,the value of ‘size’ is not usable in a constant expression
。什么是常量表达式?再次查看 docs:
定义一个可以在编译时计算的表达式。
重要的是要注意这里的声明:
const unsigned int size = x.size();
不是这里的问题,这个变量 size
的用法是问题。 size
的值在编译时不可用,因此不能用于创建数组。因此有以下一行:
std::array<FV,size> X;
不正确。要解决此问题,只需使用 SIZE
模板参数而不是 size
。
一旦完成,错误就会消失,但会带来新的错误;)
./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()’
最后一行告诉我们问题:error: no matching function for call to ‘FV::FV()’
。我们在 class FV
中没有默认构造函数。让我们创建一个:
class FV {
public:
....
FV() = default;
};
一旦修复,最后一个错误仍然存在:
./x.cpp:47:16: error: type ‘class FV’ argument given to ‘delete’,expected pointer
47 | delete fourierVariables;
| ^~~~~~~~~~~~~~~~
您正试图删除一个非指针变量,这是无效的。只需删除该语句,您的程序就会编译。
我再次建议你多学习 C++,或者至少学习你正在使用的东西和工具,这样你至少可以找出什么时候出现问题。不要使用你不理解的东西,它只会给你带来问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。