i'm new to c++ and i'd like to implement the observer pattern.
i made an Observer.h and an Listener.h file but in my class that include those leaders, i try to implement the methods defined in the header files without success.
i dont understand if i have to use pointers or so but there's my implementation of the Observer.h notifyAll method for instance :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include "Observer.h"
...
void Observer::addListener(Listener listener){
listeners.push_back(listener);
}
void Observer::removeListener(Listener listener){
// TODO
}
void Observer::notifyAll(){
for (Listener l in listeners) {
l.update();
}
// for (int i=0; i<listeners.size(); i++){
// listeners[i].update();
// }
}
Linker errors mean that you have not linked a library (or your IDE hasn't), or you haven't compiled the file that has Listener in it for example. Edit: So check you are compiling all the relevant files, and have settings to link all the relevant libraries.
This Part: for (Listener l in listeners)
is definitely psuedo code, and is definitely not C++, as Moschops has also said. It does look at bit like VBA though.
oh ok, but in methods above (addListener & removeListener) the compiler doesn't complaint about Listener object so i guess the link is right, isn't it ?
i've done this test and the compiler gives no errors:
the compiler doesn't complaint about Listener object so i guess the link is right, isn't it ?
But it does complain:
Apple Mach-O Linker Error - Undefined symbols for architecture i386:
"Listener::update()", referenced from:
Observer::notifyAll() in testApp.o
This is saying that it cannot find the Listener::update() function, which belongs to the Listener class. Check that the update function exists in the Listener header and Listener.cpp files.
Well there you go - there is no code for the function, hence the errors.
Normally you have your class definition in the header file and the implementation of the function in the .cpp file.
In my IDE, when I create a new class, it create the .h and .cpp files for me. When I create a new function in the header file, it creates a stub for me in the .cpp file. A stub is an empty function definition.
If I had to guess, you've not yet written the update function, or if you have, you're not linking to the compiled code.
i'm from the java world and thought those header files were like interfaces in java...
They can be. It's up to you to make it like that. When you #include a header file, all that happens is the entire contents of the file is copied into the place where you #include it. That's it. It's up to you to make sure that copying all that code into that place makes sense and does what you want it to.
yes, listener is an object of type Listener ("Listener.h") and yes, listeners is a vector but since i included Listener.h file, i thought i can access it can't i ?
You can only access member variables from an object like listener. listeners should have a private or protected access, not public. So you need a function inside Listener to achieve that.