passing structures as pointers through functions [ERROR] why?

Started by
12 comments, last by Morrandir 15 years, 10 months ago
Quote:Original post by Mathmo
EDIT: sorry about the double post


Hey, I'm busy working on it.

So here are my options right now.

First I'm reprogramming the class you suggested, and then if both attempts fail I'll try to make attract() a private function, since yes update() is the only one that calls it.

I would just quickly change that to private, however, I'm in the middle of debugging some smaller issues, since I no longer get that ugly debug message that started this post.

I'll keep you'll updated.
Advertisement
Just don't nestle the structs within eachother ;)
Declare struct particles before the class.
your original problem was that you had the declaration of the struct after you used in a function. You can't do that in c or c++. Things must be declared before you use them.

class Particle{private:struct particles; <-----forward declerationpublic:	Particle();	~Particle();		void reset_particles(int n);	void update(int* screen);	void reset_all();	void attract(particles *A, particles *B);	void render(int* screen);private:	struct particles	{		double mass;		double x, y;		long oldX, oldY;		long xp, yp;		double ax, ay, vx, vy;	}p[NUM];	int cx, cy;};


this should compile however there's no point to making that an inner struct, and a private one at that.

[Edited by - fishmd on June 5, 2008 10:25:14 AM]
You can keep the nested struct declaration if you really want to.

Your member function declaration should look like this:
void attract(particles *A, particles *B);


Your member function definition should look like this:
void Particle::attract(Particle::particles *A, Particle::particles *B){  // ...}


If you fixed the other typos you had there (you mistyped some of your variable names), then this should work fine.

EDIT: fishmd is right about the declaration order, I wonder why the compiler didn't complain about using an undeclared type.

This topic is closed to new replies.

Advertisement