How to fix "expression must have integral or unscoped enum type"

I have the following code and trying to perform a simple calculation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cmath>
#include <math.h>
#include "fftw3.h"
using namespace std;
int main(){
 double CFL = 1.;
 double x0 = 64.;
 double y0 = 64.;

 double *XX;
 XX = (double*)fftw_malloc(nx*ny*sizeof(double));
 memset(XX, 42, nx*ny* sizeof(double));

 double *YY;
 YY = (double*)fftw_malloc(nx*ny*sizeof(double));
 memset(XX, 42, nx*ny* sizeof(double));

 // ERROR HERE
 double theta = std::atan2(XX-x0, YY-y0)-(2*M_PI/24);
 // more lines
}


I am getting the error:
 
expression must have integral or unscoped enum type


This error shows in x0 and y0, I tried using atan2 as well without std:: but still same problem. Do I have to convert my XX and YY or x0 and y0?
Hello JamieAl,

To begin with "cmath" and "maht.h" are the same thing, but you should just use "cmath" for a C++ program.

You define "XX" and "YY" as pointers. these variables only hold an address. in line 19 you are trying to subtract "64.0" from an address.

Untested but try double theta = std::atan2(*XX-x0, *YY-y0)-(2*M_PI/24);. I do believe that you have to dereference the pointer to get to the value it points to.

Andy
Hello JamieAl,

Went to test your program, but found it is missing the header file "fftw3.h".

Also you need to include "cstring" to use "memset". At least I had to in MSVS 2017.

Andy
JamieAl,
First, you are doing memset(array_of_doubles, 42, size of the array);This is converting 42 as a raw byte into each byte of your array of doubles, which is just making it junk since you shouldn't directly overwrite the bytes of doubles.

Second, you didn't explain what you want line 19 to do. As Andy explained, XX are pointers, but you are trying to subtract a double from them.

If you are trying to do atan2(y[i] - y0, x[i] - x0) for each element of the array, then you need to use a for loop.
Last edited on
double theta = std::atan2(XX-x0, YY-y0)-(2*M_PI/24);

@JamieAl
You are trying to translate what is a perfectly legitimate array expression in Matlab, Fortran or Python ... directly into C++.

Sadly, most C++ arrays don't work like that (unless they are valarrays). As Ganado says, in C++ if you keep this type of array then you are stuck with writing a loop.
Thanks everyone! Your suggestion were all helpful. I am in the process of transitiong from MATLAB to C++ and it has been quiet challeneging so I appreaciate any help.

@Handy Andy
Went to test your program, but found it is missing the header file "fftw3.h".

Also you need to include "cstring" to use "memset". At least I had to in MSVS 2017.


You're right, I have included all these libraries in my original code and should have wrote it here in order for everyone to reproduce the code.

@Ganado and @lastchance
Thanks a lot, I wasn't 100% sure if I could do this simple like MATLAB and be understood as an array expressions. But now I used a for loop and have fixed it.
@Ganado

First, you are doing memset(array_of_doubles, 42, size of the array);This is converting 42 as a raw byte into each byte of your array of doubles, which is just making it junk since you shouldn't directly overwrite the bytes


I was under the impression that I can use memset as an initialization for my variables in code. I used 42, which was a random number that fortunately worked for me once and have been using it ever since. So I use memset to initialize with memory space (like initialize with zeros) and use for loops in order to initialize other variables with specific values and so on.
I was under the impression that I can use memset as an initialization for my variables in code.
Sure, that's fine. You're sort of just using it to initialize it to some well-defined, but junk value.
The value being set is the binary value of 42, repeated for 8 bytes (assuming 64-bit double), for each double in the array, which happens to be 1.42603e-105, at least on my compiler. I assume you overwrite these values with something meaningful later on before doing real calculations...? Otherwise, it's junk-in, junk-out.
Last edited on
Topic archived. No new replies allowed.