How to pass parameters to constructor of class

###ball.h

class Ball {
public:
	Ball(ofColor colors);

	void moveTo();
	void draw();

	int x;
	int y;
	ofColor color;
};

###ball.cpp

Ball::Ball(ofColor colors){
	color = colors;
	x = 0;//ofRandom( ofGetWindowWidth() );
	y = 0;//ofRandom( ofGetWindowHeight() );	
}

###testApp.h

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 windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);

		Ball theBall;		
};

###testApp.cpp

void testApp::setup(){
	theBall = Ball(ofColor::azure);
}

I get error – error C2512: ‘testApp’ : no appropriate default constructor available

Somehow it just doesn’t work. I thought passing parameters to constructors should be pretty easy but I have tried everything and am still unable to pass parameters to a constructor. Since it very easily possible in c++ I don’t see why I keep getting errors in openframeworks. Please help. I am already pretty irritated by this error and might just give up openframeworks.

Well I got the answer. Just to help any other beginners who might have the same problem you need to use pointers. Thus in testApp.h

Ball *theBall;

and in testApp.cpp:

theBall = new Ball(ofColor::crimson);

and you can access all properties of your object like this:

theBall->x
theBall->y
theBall->draw()

If you want it as an array initialize like this:

Ball** theBall;

This is an array of pointers.

1 Like

nice Q and A. Just a quick addition: when you don’t have a default constructor, you can’t create the object on the stack which is your error in the first posting. You can only create a pointer to the object, as you are doing here. In OF we typically add setup() functionality to most objects so you don’t have to deal with pointers, in this case, you’d add a function for passing in color. Pointers can be a bit trickier to manage since you have to think about allocating and deallocating memory. (they can also be super helpful once you’ve got your head wrapped around them)

actually you can create the object in the stack but only in the constructor, you can do:

testApp::testApp():theBall(ofColor::red){
}

In regards to pointers I have one more question. Since I have dynamically added an object Ball to the pointer, I would need to delete the pointer after I have used it, so it doesn’t cause any memory leak. Where do I do this? Or does openframeworks takes care of this in some way?

Thanks for this this approach. Worked great. For all beginners this is what I did.

add constructor and ball object in testApp.h
###testApp.h

class testApp : public ofBaseApp{

public:
    testApp();
    Ball theBall;

###testApp.cpp

testApp::testApp():theBall(ofColor::red){}

Then use all functions normally with dot operator

theBall.x
theBall.y

I put a delete pointer in the exit method of testApp. Is that correct?

if you are allocating in the stack like in the last example you posted, then you don’t need to use delete, only if you use new you will need delete

unless is totally necessary, which is really few times try to avoid using new, you’ll avoid some headaches with memory allocation and some things might be even faster