for each

hi,

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();
//    }
}


here's my Observer.h file :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef emptyExample_Observer_h
#define emptyExample_Observer_h



#endif
#include "Listener.h"

class Observer {
    
public:
    vector<Listener> listeners;
    void addListener(Listener listener);
    void removeListener(Listener listener);
    void notifyAll();
};



i get an error on the first snippet of code at line 13: selector element type "Listener" is not a valid object

any idea ?
Last edited on
The first part of your code seems to be psuedo code, while the commented part is the C++ code.

HTH

Please use code tags - the <> button on the right
for (Listener l in listeners)

What the bejesus is that? This is not the syntax of a C++ for loop. You need to go and read up on C++ for loops.
ok, code tags fixed but have you any answer regarding my problem ?
Last edited on
If you have fixed the problem why do you need us to give you an answer to it?
Moschops,

this is the syntax of a "for in" loop proposed by XCode.

my problem is not fixed, only the use of code tags as suggested by TheIdeasMan.

but as you suggest it, i tried with a simple for loop and i have 2 others error :
1
2
3
Apple Mach-O Linker Error - Undefined symbols for architecture i386:
  "Listener::update()", referenced from:
      Observer::notifyAll() in testApp.o
Last edited on
ok, fixed but have you any answer regarding my problem ?


I thought that I had answered your problem:

TheIdeasMan wrote:

The first part of your code seems to be psuedo code, while the commented part is the C++ code.
no this is not pseudo-code, this is a "for in" loop suggested by XCode.

here's the modified code that raises 2 errors as mentionned above :

1
2
3
4
5
void Observer::notifyAll(){
    for (int i=0; i<listeners.size(); i++){
        listeners[i].update();
    }
}
Last edited on
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.
Last edited on
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:

1
2
3
4
5
6
7
8
9
10
11
void Observer::addListener(Listener listener){
    listeners.push_back(listener);
}
void Observer::removeListener(Listener listener){
    // TODO
}
void Observer::notifyAll(){
//    for (int i=0; i<listeners.size(); i++){
//        listeners[i].update();
//    }
}
Last edited on
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.
here's my Listener.h file :

1
2
3
4
5
class Listener {
    
public:
    void update();
};


i have no Listener.cpp file and i dont' know if i need one because i'm implementing in another .cpp file (yeah i'm very new :) )

i'm from the java world and thought those header files were like interfaces in java...
Last edited on
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.

This is code that goes in the .cpp file:
1
2
3
void Observer::addListener(Listener listener){
    listeners.push_back(listener);
}


It will have a corresponding declaration inside the class in the header file.
Last edited on
so if i get you right, i'd rather make an interface regarding the observer pattern ?

another way of saying that is, do i need a .cpp file with the same name for each .h file ?
Last edited on
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.
1
2
3
void Observer::addListener(Listener listener){
    listeners.push_back(listener);
}


This looks dodgy, is listener an object of type class Listener? And is listeners a vector?

Normally, you access member variables via a class function.

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.
Topic archived. No new replies allowed.