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