0

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);
    }
}
2
  • 1
    That’s unrelated to arrays. Simply remove holder. from the front of the dft calls – it’s not a member function. Commented Oct 7, 2013 at 15:48
  • I take it back, I think we do. Commented Oct 7, 2013 at 15:49

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The calling code is expecting a Complex - it calls getReal on it.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.