Trying to add a listener to a ofParameterGroup: Invalid use of non-static member function

Hi,

I created a simple class, which simplified looks like this on the .h file:

class Canvas {

public:

    Canvas();

    Canvas(int &widthArg,
           int &heightArg);

    Canvas(ofParameter<int> &widthArg,
           ofParameter<int> &heightArg);

    int xCanvas;
    int yCanvas;
    int width;
    int height;

    int margin;
    ofColor color;
    bool visible;
    ofRectangle rect;
    ofRectangle workingArea;

}

I have different kinds of constructors there, the third one I did to pass it the values from some parameters connected to the GUI:

Canvas::Canvas(ofParameter<int> &widthArg,
               ofParameter<int> &heightArg){

    margin = ofGetHeight() / 20;
    height = heightArg;
    width = widthArg;
    xCanvas = margin;
    yCanvas = margin;
    visible = true;
    color = ofColor::yellow;
    rect.set(xCanvas, yCanvas, width, height);
    workingArea.set(rect);

}

I have the class instantiated in my ofApp.h file like this:


class ofApp : public ofBaseApp{

public:

    Layer layer;
    ofFbo fbo;
    std::vector<Layer*> layers;
    ofParameter<int>	canvasWidth{30};
    ofParameter<int> 	canvasHeight{50};

    Canvas canvas = {canvasWidth, canvasHeight};

...

In the GUI, I have this values inside a ofParameterGroup:


    canvasParameters.setName("Canvas Parameters");
    canvasParameters.add(canvasWidth);
    canvasParameters.add(canvasHeight);
    ofAddListener(canvasParameters.parameterChangedE(), this, ofApp::updateCanvas);

    canvasGroup->setConfig(ofJson({{"type", "fullsize"}, {"direction", "vertical"}}));
    canvasContainer = canvasGroup->addContainer();

    canvasContainer->add(canvasWidth.set("Width listener", canvasWidth, 10, 3000), ofJson({{"type", "fullsize"}, {"direction", "vertical"}}));
    canvasContainer->add(canvasHeight.set("Height", canvasHeight, 10, 3000), ofJson({{"type", "fullsize"}, {"direction", "vertical"}}));

Basically I want to trigger the function updateCanvas from ofApp everytime a value inside the parameter group gets changed:


void ofApp::updateCanvas(){

    canvas.updateCanvas(canvasWidth,
                        canvasHeight);

}

this is sort of a caller to the function Canvas::updateCanvas() because I was getting issues, and still am, linking it to the listener.


void Canvas::updateCanvas(ofParameter<int> &widthArg, ofParameter<int> &heightArg){

    height = heightArg;
    width = widthArg;
    color = ofColor::yellow;
    rect.set(xCanvas, yCanvas, width, height);
    workingArea.set(rect);


}

Now when I try to compile this I get the following error


/home/antonio/dev/of_v0.11.2_linux64gcc6_release/apps/myApps/theia2/src/gui.cpp:48: error: invalid use of non-static member function ‘void ofApp::updateCanvas()’
   48 |     ofAddListener(canvasParameters.parameterChangedE(), this, ofApp::updateCanvas);
      |                                                               ~~~~~~~^~~~~~~~~~~~

Any ideas on what could be wrong? It seems sort of convoluted, given the fact that in the end it will only be a rectangle that changes with the gui, but I need the custom class later to open a bitmap inside of it and do other things. Any help is much appreciated.

Thanks!

Hi, the problem has to do with the arguments of ofApp::updateCanvas
it must be

void updateCanvas(ofAbstractParameter& p){

    canvas.updateCanvas(canvasWidth,
                        canvasHeight);

}

for any listener you use, the callback function must have as argument the same type as the ofEvent to which you are listening.
In the following link you will find a thorough explanation of how the events work

cheers

Thank you a lot! I will read it and understand it before I try to implement any changes.

1 Like