Chris
Chris

Reputation: 1

Issue passing an array to a function

I'm trying to pass an array of complex numbers (I've created a class called Complex that just stores a real and imaginay part) into my dft function below and get it to spit out some results. I've tried numerous variations of x -including x[], x[3], *x to try to pass the array of data into the dft function, but I keep getting "undefined reference to 'Complex::dft(Complex*, int, int)".

I apologise in advance if I've just done something really stupid and obvious, but I've been staring at this for hours and keep drawing a blank. Any help would be greatly appreciated.

Thanks

This is my dft function:

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

This is my main programme:

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

Upvotes: 0

Views: 122

Answers (2)

Joni
Joni

Reputation: 111249

You're trying to call the dft function as if it was a member in your Complex class, but it's a free function. You should call it as a free function:

    Complex ans = dft(x,N,n);
    double ansRe = ans.getReal();
    double ansIm = ans.getImag();

Upvotes: 1

LihO
LihO

Reputation: 42083

Your dft function is not a member of Complex class:

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

so you shouldn't call it on an holder object, i.e. instead of holder.dft(x,N,n).getReal() do:

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

Also consider using std::vector<Complex> instead of C-style arrays.

Upvotes: 2

Related Questions