advancedEventsExample and ofNotifyEvent

I looked at the other posts related to ofEvents and couldnt find how to send a reference of the sender object to the listener object. So I created a new example from the advancedEventsExamples and my app began throwing these compile errors after I moved the ofNotifyEvent calls outside of the OF update event registered eventsObject::update call…

testApp.h:12: error: no matching function for call to ‘ofAddListener(ofEvent<Person*>&, testApp* const, void (testApp::*)(Person*))’

Person.h:14: instantiated from here

…/…/…/libs/openFrameworks/events/ofEventUtils.h:73: error: no matching function for call to ‘ofEvent<Person*>::notify(Person*&, Person* const&)’

…/…/…/libs/poco/include/Poco/AbstractEvent.h:204: note: candidates are: void Poco::AbstractEvent<TArgs, TStrategy, TDelegate>::notify(const void*, TArgs&) [with TArgs = Person*, TStrategy = Poco::FIFOStrategy<Person*, Poco::AbstractDelegate<Person*>, Poco::p_less<Poco::AbstractDelegate<Person*> > >, TDelegate = Poco::AbstractDelegate<Person*>]

testApp.h

  
  
#ifndef _TEST_APP  
#define _TEST_APP  
  
#include "ofMain.h"  
#include "ofEvents.h"  
  
#include "Person.h"  
  
class testApp : public ofBaseApp{  
public:  
	void testApp::setup(){  
		ofAddListener(person0.walkedIn, this, &testApp::personWalkedIn);  
	}  
	  
	void testApp::update(){ }  
	void testApp::draw(){ }  
	  
	void testApp::mousePressed(int x, int y, int button) { person0.walkIn(); }  
	  
	void testApp::personWalkedIn(Person *p){ cout<<p->name<<" walked in"<<endl; }  
	  
	Person person0;  
};  
#endif  
  

Person.h

  
  
#ifndef _OF_PERSON_EVENTS  
#define _OF_PERSON_EVENTS  
  
#include "ofMain.h"  
#include "ofEvents.h"  
  
class Person {  
public:  
	Person(){   
		id = 0;  
		name = "person_0";  
	}  
	  
	void walkIn() { ofNotifyEvent(walkedIn, this, this); }  
  
	int id;  
	string name;  
	  
	ofEvent<Person*> walkedIn;  
};  
#endif  
  

I also tried sending via args and got different compile errors. and then tried sending and id and name via args and it ran and compiled! so i may post that code afterwards… but i wanted to see why this isnt compiling first.

Thanks,

Ekene

This weekend I didnt have WIFI so I couldnt Google the “error: ISO C++ forbids declaration of ‘Person’ with no type” error the app was throwing. So today I added the “class Person;” line to PersonEvents.h and its running! This example sends a pointer of the sender object to the listener method using an ofEventArgs like object but Itd be great to know if and how it can be sent without using args. Also I attached both of the examples as zipped xcode projects.

testApp.h

  
  
#ifndef _TEST_APP  
#define _TEST_APP  
  
#include "ofMain.h"  
  
#include "Person.h"  
#include "PersonEvents.h"  
  
class testApp : public ofBaseApp{  
public:  
	void setup(){  
		ofAddListener(person0.walkedIn,this,&testApp::personWalkedIn);  
		ofAddListener(person0.walkedOut,this,&testApp::personWalkedOut);  
	}  
	  
	void update(){ }  
	void draw(){ }  
	  
	void mousePressed(int x, int y, int button){ person0.walkIn(); }  
	void mouseReleased(int x, int y, int button){ person0.walkOut(); }  
	  
	void personWalkedIn(PersonEventArgs & e){ cout<<e.person->name<<" walked in"<<endl; }  
	void personWalkedOut(PersonEventArgs & e){ cout<<e.person->name<<" walked out"<<endl; }  
	  
	Person person0;  
};  
#endif  
  

Person.h

  
  
#ifndef _OF_PERSON  
#define _OF_PERSON  
  
#include "PersonEvents.h"  
  
class Person {  
public:  
	Person(){  
	    id = 0;  
		name = "person_0";  
	}  
	  
	void walkIn() {  
		PersonEventArgs e;  
		e.person = this;  
		  
		ofNotifyEvent(walkedIn, e, this);  
	}  
	  
	void walkOut() {  
		PersonEventArgs e;  
		e.person = this;  
		  
		ofNotifyEvent(walkedOut, e, this);  
	}  
	  
	ofEvent<PersonEventArgs> walkedIn;  
	ofEvent<PersonEventArgs> walkedOut;  
	  
	float id;  
	string name;  
};  
#endif  
  

PersonEvents.h

  
  
#ifndef _OF_PERSON_EVENTS  
#define _OF_PERSON_EVENTS  
  
#include "Person.h"  
  
class Person;  
  
class PersonEventArgs : ofEventArgs {  
public:  
	int id;  
	string name;  
	Person * person;  
};  
#endif  
  

Hey I stumbled up on this old post because I had the same problem.
The solution is actually easier than creating a new class.

this seems to be a pointer so you need to pass its value.

void walkIn() { ofNotifyEvent(walkedIn, *this, this); }

kind of interesting! can you post a little example?