views:

73

answers:

3

Hello All I have a question which I cannot seem to fathom out, as fairly newish to c++. I have a class, in which a set of variables are declared in the .h file and then initialised in the .cpp file. These same variables are used by a set of 3 other classes - and the compiler has them as out of scope. I am not sure how to link the classes so that the variables are visible by all classes. The code itself is a port from a java based language. I am using openFrameworks as my development environment, and I posted my compiler errors on the forum there if it's helpful to have a look http://www.openframeworks.cc/forum/viewtopic.php?f=8&t=4505

smoke.h

#pragma once

#ifndef SMOKE
#define SMOKE

#include "ofMain.h"
#include "VSquare.h"
#include "VBuffer.h"
#include "Particle.h"

#define LWIDTH 151
#define LHEIGHT 11

class Smoke {
public:
int WIDTH;
int HEIGHT;

int RES;
int PENSIZE;


int PNUM;


VSquare v [LWIDTH] [LHEIGHT] ;
VBuffer vbuf [LHEIGHT][LHEIGHT] ;

Particle p [30000];

int pcount;
int mouseXvel;
int mouseYvel;

int randomGust;
int randomGustMax;
float randomGustX;
float randomGustY;
float randomGustSize;
float randomGustXvel;
float randomGustYvel;

Smoke();

void init();
void draw();

};
#endif

The code in my .cpp file looks like this:

#include "Smoke.h"


Smoke::Smoke(){
WIDTH = 300; //these are the variables that go out of scope in all other classes
HEIGHT = 300;

RES = 2;
PENSIZE = 30;


PNUM = 30000;

pcount = 0;
mouseXvel = 0;
mouseYvel = 0;

randomGust = 0;
}

and one of the classes that I have a problem with:

Particle.h

#ifndef PARTICLE
#define PARTICLE

#include "ofMain.h"


class Particle{
public:
float x;
float y;
float xvel;
float yvel;
float temp;
int pos;

Particle(float xIn = 0, float yIn = 0);

void reposition();
void updatepos();



};
#endif

and the .cpp file where the errors are thrown (excerpt):

#include "Particle.h"

Particle::Particle(float xIn, float yIn){
x = xIn;
y = yIn;

}

void Particle::reposition() {
x = WIDTH/2+ofRandom(-20,20); //eg, WIDTH and HEIGHT not declared in this scope
y = ofRandom(HEIGHT-10,HEIGHT);

xvel = ofRandom(-1,1);
yvel = ofRandom(-1,1);
}

void Particle::updatepos() {
int vi = (int)(x/RES); //RES also not declared in this scope
int vu = (int)(y/RES);

if(vi > 0 && vi < LWIDTH && vu > 0 && vu < LHEIGHT) {
v[vi][vu].addcolour(2);
//and so on...

Really hoping this is something that folks can help me figure out! Thanks so much

A: 

The variables are declared within the scope of the Smoke class, so a) they're not global and b) you'll need an instance of a Smoke object in order to get your hands on them. You'd need to #include "Smoke.h", and create / obtain an instance of a Smoke object and then use e.g. mySmoke.WIDTH.

If you want the variables to be 'global' then you need to declare them outside of the scope of the Smoke class - but you'll still need to #include the relevant header.

Will A
+1  A: 

If the WIDTH and HEIGHT are supposed to be constants, you can move them out of Smoke and just declare them as constants: const int WIDTH = 300; If they need to be members of Smoke but are still constants, then just declare them as static const int WIDTH = 300 then #include "Smoke.h" in your Particle.cpp and reference it as Smoke::WIDTH. This is like a public static variable in Java. If each Smoke object needs its own width, then you need to tell the Particle object which Smoke it needs, or pass in the WIDTH and HEIGHT to the Particle constructor.

dash-tom-bang
Thanks for the clear answer- this was really helpful- now I realise where I was going wrong!
sacculi
A: 

An instance of the Smoke class will be needed if you would like to access WIDTH AND HEIGHT as they are currently defined because they are part of the smoke class.

Change your method to receive a Smoke instance and then use it in the method as follows:

void Particle::reposition(Smoke& smoke) {  
x = smoke.WIDTH/2+ofRandom(-20,20); //eg, WIDTH and HEIGHT not declared in this scope  
y = ofRandom(smoke.HEIGHT-10, smoke.HEIGHT);  

xvel = ofRandom(-1,1);  
yvel = ofRandom(-1,1);  
}  

int main() {  
Smoke smoke;  
Particle particle;  
particle.reposition(smoke);  
}  
skimobear