gpt4 book ai didi

c++ - 将数组传递给函数的问题

转载 作者:行者123 更新时间:2023-11-28 00:40:17 25 4
gpt4 key购买 nike

我正在尝试将复数数组(我创建了一个名为 Complex 的类,它只存储实部和虚部)传递到下面的 dft 函数中,并让它输出一些结果。我尝试了 x 的多种变体 - 包括 x[]、x[3]、*x 以尝试将数据数组传递到 dft 函数中,但我不断收到“ undefined reference ” Complex::dft(Complex*, int, int)”。

如果我刚刚做了一些非常愚蠢和明显的事情,我会提前道歉,但我已经盯着它看了好几个小时并且一直在画空白。任何帮助将不胜感激。

谢谢

这是我的 dft 函数:

#include <iostream>
#include "Complex.h"
#include "ofstream_add.h"
#include <cmath>
const double PI=4*atan(1.0);

Complex dft(Complex x[], int N, int n)
{
Complex holder;
Complex sum = Complex(0,0);
Complex temp;

for (int k=0; k<N; k++)
{
temp = Complex(cos(2.0*PI*k*n/N),-sin(2.0*PI*k*n/N));
sum = sum + x[k] * temp;
}
double conv = N; //convert integer to double as complex takes in doubles
Complex complexN((1/conv),(1/conv));
holder = complexN * sum;

return holder;
}

这是我的主要程序:

#include <iostream>
#include <iomanip>
#include "Complex.h"
#include "ofstream_add.h"

int main()
{
int N = 3;
Complex holder;
Complex y[N];
double re,im,ansRe,ansIm;

Complex a(2,1.7);
Complex b(3.5,1.2);
Complex c(4.2,2.3);
Complex x[3] = {a, b, c};

for (int n=0;n<N;n++)
{
//does some processing on the array of complex numbers an spits out a
//real and imaginary part
double ansRe = holder.dft(x,N,n).getReal();
double ansIm = holder.dft(x,N,n).getImag();

//stores the result in a new complex number
y[n].setReal(ansRe);
y[n].setImag(ansIm);
}
}

最佳答案

你的 dft函数不是 Complex 的成员类:

Complex dft(Complex x[], int N, int n)
{
...
}

所以你不应该在 holder 上调用它对象,即代替 holder.dft(x,N,n).getReal()做:

double ansRe = dft(x,N,n).getReal();

也可以考虑使用 std::vector<Complex>而不是 C 风格的数组。

关于c++ - 将数组传递给函数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19229069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com