'complex' does not name a type

Hello,
I am running IDE: Code::Blocks 16.1 With this compiler : mingw-get version O.6.2-beta-20131004-1
This code produces an errors.
FFT C Rosetta Code FFT
error: 'complex' does not name a type
error: variable or field '_fft' declared void
error: 'cplx' was not declared in this scope
error: 'cplx' was not declared in this scope
error: expected primary-expression before 'int'
error: expected primary-expression before 'int'
The one that is particularly puzzling is the first one since the same code seems to work in another program.
Does anyone have any thoughts?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <math.h>
#include <complex.h>

using namespace std;

//typedef double complex cplx;  // same syntax
typedef complex<double> cplx;

// error: 'complex' does not name a type
double PI;
//


//typedef complex  double  cplx;

void _fft(cplx buf[], cplx out[], int n, int step)
{
	if (step < n) {
		_fft(out, buf, n, step * 2);
		_fft(out + step, buf + step, n, step * 2);

		for (int i = 0; i < n; i += 2 * step) {
			cplx t = cexp(-I * PI * i / n) * out[i + step];
			buf[i / 2]     = out[i] + t;
			buf[(i + n)/2] = out[i] - t;
		}
	}
}

void fft(cplx buf[], int n)
{
	cplx out[n];
	for (int i = 0; i < n; i++) out[i] = buf[i];
	_fft(buf, out, n, 1);
}

void show(const char * s, cplx buf[]) {
	printf("%s", s);
	for (int i = 0; i < 8; i++)
		if (!cimag(buf[i]))
			printf("%g ", creal(buf[i]));
		else
			printf("(%g, %g) ", creal(buf[i]), cimag(buf[i]));
}

int main()
{
	PI = atan2(1, 1) * 4;
	cplx buf[] = {1, 1, 1, 1, 0, 0, 0, 0};
	show("Data: ", buf);
	fft(buf, 8);
	show("\nFFT : ", buf);
	return 0;
}

This code with the same syntax does not have the error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <complex>
using namespace std;
typedef complex <double> dcmplx; // same syntax

int main(){
  dcmplx a(5.,6.),b;
  cout << "Enter b: ";
  cin >> b;
  cout << "a = " << a << "\n";
  cout << "b = " << b << "\n";
  cout << "a + b = " << a + b << "\n";
  cout << "a * b = " << a * b << "\n";
  cout << "a / b = " << a / b << "\n";
  cout << "|a| = "   << abs(a) << "\n";
  cout << "complex conjugate of a = " << conj(a) << "\n";
  cout << "norm of a = " << norm(a) << "\n";
  cout << "abs of a = " << abs(a) << "\n";
  cout << "exp(a) = " << exp(a) << "\n";
}

Last edited on
The first code is in C, not C++. I don't think complex was a type (actually, a macro) until C-99 and the format here would have been
double complex
and not
complex<double>

The C++ version (see the reference on this site) is as in the second snippet of code:
complex<double>
or
complex<float>
I would go with the C++ version if possible.

Those FFT routines are pretty neat, though!




Thank you very much for the help.
I am new to C++, but have some experience with C.
It was my understanding that C is a subset of C++
So C code can be compiled on a C++ compiler. Is this true?
I am using the same code in each example. - typedef complex<double> cplx;
One compiles and runs, the other reports this error - error: 'complex' does not name a type
These are other errors some of them are likely related.
error: variable or field '_fft' declared void
error: 'cplx' was not declared in this scope
error: 'cplx' was not declared in this scope
error: expected primary-expression before 'int'
error: expected primary-expression before 'int'

When you say "The C++ version (see the reference on this site) is as in the second snippet of code:
complex<double>" are you referring to FFT code?
Standard C is almost a subset of C++. Complex type was not available in the original C - it came in within C-99, a later incarnation. C99 is NOT a subset of C++.

complex<double> is the C++ form, NOT the C-99 form, which I said in my first response.

The FFT code looks to be mainly C code, but could be easily converted to C++ by observing the correct definition of type and naming of the relevant functions.



Thank you again for all the help.
When I commented out the C++ code and un-commenting the C code this code compiled and ran in a C project with Code:Blocks and the gcc compiler .
I had been thinking I could compile any C code as C++ code.
But I guess that is not the case.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <math.h>
#include <complex.h>

//using namespace std;           // C++ code

typedef double complex cplx;     // C code
//typedef complex<double> cplx;  // C++ code
double PI;
//


//typedef complex  double  cplx;

void _fft(cplx buf[], cplx out[], int n, int step)
{
	if (step < n) {
		_fft(out, buf, n, step * 2);
		_fft(out + step, buf + step, n, step * 2);

		for (int i = 0; i < n; i += 2 * step) {
			cplx t = cexp(-I * PI * i / n) * out[i + step];
			buf[i / 2]     = out[i] + t;
			buf[(i + n)/2] = out[i] - t;
		}
	}
}

void fft(cplx buf[], int n)
{
	cplx out[n];
	for (int i = 0; i < n; i++) out[i] = buf[i];
	_fft(buf, out, n, 1);
}

void show(const char * s, cplx buf[]) {
	printf("%s", s);
	for (int i = 0; i < 8; i++)
		if (!cimag(buf[i]))
			printf("%g ", creal(buf[i]));
		else
			printf("(%g, %g) ", creal(buf[i]), cimag(buf[i]));
}

int main()
{
	PI = atan2(1, 1) * 4;
	cplx buf[] = {1, 1, 1, 1, 0, 0, 0, 0};
	show("Data: ", buf);
	fft(buf, 8);
	show("\nFFT : ", buf);
	return 0;
}

Topic archived. No new replies allowed.