Array Problem [UPDATE]

I am fallowing the tutorial on wiki and at the moment having problems with the array in the code I added. I can add as many balls as I can with manually but when I try to add them with an array it give errors.

http://www.tolgainam.net/balls.zip

can you be more specific when you post about what kind of errors you get, etc ? just posting a zip isn’t the friendliest way to report your problem. say for example, the compiler gave this error:

error 2343: …

or a screenshot of the error, etc, and we can try to help you read what it is saying so you can fix problems on your own.

check your h file, there is no array there:

  
class testApp : public ofBaseApp{  
  
	public:  
  
		void setup();  
		void update();  
		void draw();  
  
		void keyPressed  (int key);  
		void keyReleased(int key);  
		void mouseMoved(int x, int y );  
		void mouseDragged(int x, int y, int button);  
		void mousePressed(int x, int y, int button);  
		void mouseReleased(int x, int y, int button);  
		void resized(int w, int h);  
  
		ofVideoPlayer 		fingerMovie;  
		bool                frameByframe;  
};  

you can’t use it in the class if it’s not defined in the h file:

  
  
//--------------------------------------------------------------  
void testApp::update(){  
  
  
  
	for(int i=0; i<AMOUNT; i++)  
	{  
		myBall[AMOUNT].update();  
	}  
	  
  
}  
  

  • zach

The first one is not my h file, mine is:

  
  
#ifndef _TEST_APP  
#define _TEST_APP  
  
  
#include "ofMain.h"  
#include "ball.h"  
  
#define AMOUNT 5  
  
class testApp : public ofSimpleApp{  
  
	public:  
		void setup();  
		void update();  
		void draw();  
  
		void keyPressed  (int key);  
		void keyReleased(int key);  
		void mouseMoved(int x, int y );  
		void mouseDragged(int x, int y, int button);  
		void mousePressed(int x, int y, int button);  
		void mouseReleased(int x, int y, int button);  
		void windowResized(int w, int h);  
  
		_ofBall myBall[AMOUNT];  
		  
	private:  
  
};  
  
#endif  
  

And in my cpp

  
  
...  
  
void testApp::update(){  
  
  
  
	for(int i=0; i<AMOUNT; i++)  
	{  
		myBall[AMOUNT].update();  
	}  
	  
  
}  
...  
  

This is what I got at the end a blank screen and:

  
'balls_debug.exe': Loaded 'C:\WINDOWS\system32\oleaut32.dll'  
HEAP[balls_debug.exe]: Heap block at 01766E38 modified at 01767280 past requested size of 440  
Windows has triggered a breakpoint in balls_debug.exe.  
  
This may be due to a corruption of the heap, which indicates a bug in balls_debug.exe or any of the DLLs it has loaded.  
  
This may also be due to the user pressing F12 while balls_debug.exe has focus.  
  
The output window may have more diagnostic information.  
The program '[19376] balls_debug.exe: Native' has exited with code 0 (0x0).  
  

then the window freezes and ofAppGlutWindow.cpp opens in the editor
indicating this line of code:

default:
return CallWindowProc(currentWndProc, handle, Msg, wParam, lParam);
break;

this:

  
myBall[AMOUNT].update();  

should be:

  
myBall[i].update();  

if not, you’re always accessing a position after the end of the array which is AMOUNT - 1

ps: sorry ! I had a few testApp.h files open in my editor :wink:

Okay I made it work yesterday and it was a nice moment for me to seeing the circles jumping around in the screen.

Now what I am trying to do is adding an extra ball to screen whenever I press a keyboard button.

testApp.cpp ->

  
void testApp::setup(){  
  
ofSetWindowTitle("Balls");  
ofEnableSmoothing();  
  
nBalls = AMOUNT;  
//nBalls = 5; // the number of ball objects we want to create  
  
	myBall = new _ofBall*[nBalls];   // an array of pointers for the objects  
	  
		for (int i = 0; i < nBalls; i++){  
			float x = 20+(100*i);     //using the value of the counter variable(i) to differentiate them  
			float y = 20+(100*i);  
			int dim = 10+(i*10);  
  
			myBall[i] = new _ofBall(x,y,dim);  //create each object from the array  
		}  
  
  
//myBall = new _ofBall(ofRandom(300,400), ofRandom(200,300), ofRandom(1,10));  
  
//ofNoFill();  
  
}  

for adding balls at Setup, btw AMOUNT is 5 in the .h file

and rest is:

  
void testApp::update(){  
  
for (int i = 0; i < AMOUNT; i++){  
			myBall[i]->update();  
		}  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
for (int i = 0; i < AMOUNT; i++){  
			myBall[i]->draw();  
		}  
  
}  
  
//--------------------------------------------------------------  
void testApp::keyPressed(int key){  
  
	//_ofBall.push_back(myBall);  
	cout<< nBalls <<endl;  
	  
  
	//float x = 20+(ofRandom(0,100));  
	//float y = 20+(ofRandom(0,100));  
	float x = 100;  
	float y = 100;  
	int dim = ofRandom(40,100);  
  
	myBall[nBalls+1] = new _ofBall(x,y,dim);  
	nBalls++;  
  
	cout<< nBalls <<endl;  
  
  
}  
  

the program compiles and runs withtout an error and when I press a key
I see in the window that nBalls is increasing one by one BUT the ball does not appear in the screen.

Btw I read somewhere few minutes ago that using VECTORS is much more better then using them method I am using can anyone point me to a place where I can see some examples about using vectors ?

Thanks.