I’m trying to create a class for the first time in oF and am getting the “request for member of non-class type” error when trying to call its member functions. This makes me think the constructor is not being called correctly, which I tested by adding a cout line to the end of it.
The class is defined in separate files as a member of ofBaseApp. I tried at first having it as a member of testApp, but wasn’t sure how to access the mouseX and mouseY variables that way. Regardless, I get the same errors.
I’ve included my classes .h file in testApp.h and main.cpp and instantiated it in testApp.h in the form of “someClass *myClass;”. I call the constructor in testApp::setup() as follows: "someClass *myClass = new someClass(parameter A, parameter B, etc.).
Any idea what I’m doing wrong here? I couldn’t find an example of working with multiple classes in oF, which would be very useful to folks like me with little previous experience in C++.
I’m a little confused by the example because testApp.cpp never calls the constructor for the ball object. My class does most of the drawing in the constructor (which takes parameters for size, position, and color), then updates private variables and redraws them in response to mouse events. Therefore, I need access to testApp’s mouseX and mouseY inside the new class. In Java I would use parent.mouseX, but am not sure how to do this in C++.
Btw, I got into oF through your workshop at NY Resistor. Any NYC knitting circles coming up this fall?
You can either use the dot operator (for objects on the stack) or the arrow operator -> (for objects on the heap). But both ways will call the constuctor.
In your header file you have defined a pointer to SomeClass (to be created on the heap). Then you create a new instance with:
My point was that in the linked example, the constructor does not appears to have been called. The header file has:
ofBall myBall1;
but testApp.cpp does not have:
myBall1 = new ofBall();
Now that I don’t have my class as a member of ofBaseApp, I get an error in the form of, “No matching function for call to ‘ofBall::ofBall();’” when I did define a constructor in an included file. As mentioned originally, I checked that the constructor is not being called by adding a call to cout in it. In testApp, I used exactly the syntax above, except that there are parameters for the constructor- ints and arrays of unsigned chars for colors.
This is what I was referring to when I said it does not call the constructor. The errors I was getting are in my own project (where I do call the constructor in the .cpp file)- I am not trying to compile this example. I am just wondering why Xcode is telling me it can’t find my constructor, when it’s defined exactly as in the example- accept in a separate header file, which I included. I’m attaching the source.
Alright, so I declared myFunction as a pointer and now I get errors when calling it’s member functions: “request for member ‘drag’ in ‘((testApp*)this)->testApp::myFunction’, which is of non-class type ‘functionGraph*’”
Yes, that was the problem. I had used the dot operator. Definitely need to read up on the whole stack/heap distinction.
Lastly, how do I access testApp’s mouseX and mouseY from my class? In Java I would use parent.mouseX, but I understand it works a little differently in C++.
There’s two ways to get the mouseX, mouseY positions. 1. If it works for your setup, it’s simplest to just have the application pass them to the Ball instance in the update() of your application like
Thanks, Joshua. Finally got it compiled, although I’ll have to do some debugging from here…
First, I want to make sure I understand the whole stack/heap thing. Am I correct that I only need to delete public variables declared with a pointer? For instance, I would delete the unsigned chars passed to my constructor, but not those that take on their values inside the class?
Ok, clearly I don’t understand this fully as I am only getting the red values from my colors. I swear I read this http://www.cplusplus.com/doc/tutorial/pointers/, but it doesn’t cover passing objects to functions.
I just learned of ofColor, so rewrote everything to use that. It compiles, but crashes upon launch. This is basically it:
in the first version, lcolor is a pointer a color object, which hasn’t been allocated. in the second one, we allocate a color object and set the pointer equal to the position where we allocate.
however, unless you need it as a pointer, you don’t need to use ofColor like that, you can always do:
Thanks, Zach! I didn’t realize it would work to pass ofColor by copy. I thought I had to pass them as a pointer based on the trouble I ran into passing unsigned chars.