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;
};
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.
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)
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?
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