Expected Primary Expression Error

Hello everyone, I am new to the forums. I am having some issue with this function I have written. There are a few errors I am having trouble understanding. The frustrating part about this is that this code works when it is not a function and part of a different program. Any way here is the list of errors that it is throwing (I am using g++ to compile on linux)

error2.cpp: In function ‘void sinembed(double*, double*, double, int, int)’:
error2.cpp:60: error: expected primary-expression before ‘=’ token
error2.cpp:60: error: expected `)' before ‘;’ token
error2.cpp:60: error: invalid type argument of ‘unary *’
error2.cpp:60: error: expected `;' before ‘)’ token
error2.cpp:61: error: expected primary-expression before ‘=’ token
error2.cpp:61: error: expected `)' before ‘;’ token
error2.cpp:61: error: invalid type argument of ‘unary *’
error2.cpp:61: error: expected `;' before ‘)’ token

And here is the problematic code:
1
2
3
4
5
6
7
8
9
10
11
void sinembed(double x[MAX], double y[MAX], double amplitude, int templength, int delay)
{
	int count = 0;
	for(int n=0; n<MAX; n++){
		if (count<templength){
			x[n] += amplitude*sin((2*pi*n)/51);
			y[n+delay] += amplitude*sin((2*pi*n)/51);
		}
	count++;
	}
}


Also, feel free to give me feedback on the way I wrote this function. Thanks!
Did you do using namespace std;? If you did, I think there's a clash between your int count, and std::count().
Yes I always use that. Can I solve this by simply declaring the counter variable as something else?
Yeah. Although it would be a good idea to unlearn using so that you don't get weird errors like this.
double x[MAX] i dont think you can do that if MAX is not constant
MAX is defined as a global constant.
#define MAX 1024
For future reference the lines 60 and 61 are the same thing as lines 6 and 7. Any documentation on how to unlearn using ?

By the way I altered the declaration of the counter variable and I am still having the same issue, so it may not be a problem associated with using namespace std;
Next time mention that from the beginning.

All I can see is that either sin() isn't declared (i.e. you're not including cmath), or 'pi' isn't declared.
Both also declared. Pi is defined globally like MAX and I have included cmath like this:
#include <cmath>
I'm going to repost this in the General C++ forums.
Don't repost it. We see the question here just fine.

You need to explain to us what these globals are. How is 'pi' defined? Can you paste its definition line in here?

Also:

1
2
3
4
5
6
7
8
	int count = 0;
	for(int n=0; n<MAX; n++){
		if (count<templength){
			x[n] += amplitude*sin((2*pi*n)/51);
			y[n+delay] += amplitude*sin((2*pi*n)/51);
		}
	count++;
	}


1) if y[] is of size MAX, then y[n+delay] might step outside of the array (heap corruption) if 'delay' is anything other than zero.

2) why do you have 'count' here? It looks like count==n. Why not just use n?

3) why loop through MAX times if you only do something for 'templength' times? Why not just loop to templength?
Sorry about that I already reposted it. I didn't know if anybody cared about it in here anymore.

I did find out that my definition of pi does cause problems.

I defined it like this globally (outside of all functions):
#define pi 3.14...

But I change the subroutine to look like this and it works now:
1
2
3
4
5
6
7
8
9
10
11
12
13
void sinembed(double x[MAX], double y[MAX], double amplitude, int templength, int delay)
{
	double pi = 3.141592653589793238462643383279502;
	int ct = 0;
	double temp;
	for(int n=0; n<MAX; n++){
		if (ct<templength){
			x[n] += amplitude*sin((2*pi*n)/51);
			y[n+delay]+= amplitude*sin((2*pi*n)/51);
		}
		ct++;
	}
}


It might have something to do with my comical definition of pi. I wanted it to be as accurate to pi as possible.
Yeah I was also thinking about the delay stepping outside of the array. I was hoping that would just do nothing, i.e. if the stepping is larger than max it will just stop modifying the array. I am not experienced enough to know how that works.
I was hoping that would just do nothing, i.e. if the stepping is larger than max it will just stop modifying the array.


Nope. It causes the worst possible bug: memory corruption.

If you're lucky, it will crash your program.

If you're unlucky, it will make your program do weird things

If you're REALLY unlucky, it will work normally until 2 months later when you add 1 line of unrelated code and then your whole program will fall apart.

Heap corruption is the hardest kind of bug to find and fix. Always be weary of stepping out of bounds of arrays or using bad pointers. It's a killer.
That's not good news.

Ideally I would like limitless arrays. But in this case, I need a suggestion as to how I would input the sine wave at a delay like I have done here. I have a basic if statement idea but its going to get ugly.
Ideally I would like limitless arrays.
That's impossible. Above all, because no real computer has infinite memory, but also because arrays don't grow.
Good point. Aren't dynamic arrays or pointer arrays virtually infinite? Well even if they are, I'm far too inexperienced to rewrite everything using them.
Topic archived. No new replies allowed.