1

i am new to c++ programming and now faceing this "simple" problem for a while. I am implementing a simple step of a Observer-Pattern with the classes: Observer and Observable.

#ifndef OBSERVER_H
#define OBSERVER_H

#include "observable.h"

class Observer{

public:
  virtual ~Observer() = default;
  virtual void update(Observable* obs ) = 0;

};

#endif // OBSERVER_H

the Observabel Class Looks like that:

#ifndef OBSERVABLE_H
#define OBSERVABLE_H

#include <vector>
#include "observer.h"


class Observable
{

public:
    Observable();


    virtual ~Observable() = default;
    void attach(Observer &o);
    void detach(Observer &o);
    void notify();

private:  
     std::vector<Observer*> observerlist;
};

#endif // OBSERVABLE_H

c++- file

#include "observable.h"
#include <algorithm>


void Observable::attach(Observer &o) { observerlist.push_back(&o); }

void Observable::detach(Observer &o)
{
    observerlist.erase(std::remove(observerlist.begin(), observerlist.end(), &o));
}

void Observable::notify()
{
    for (Observer* obs : observerlist) {

        obs->update(this);                      // Here the IDE Shows the Error
    }
}

Error:

C:\U...\observable.cpp:16: error: C2660: "Observer::update": function does not take 1 Argument

I really hope one of you can help.

greetings

MasyM0
  • 11
  • 1
  • Is the error message the *only* output you get when you build? There's no informational notes or other hints about the problem? Please [edit] your question to include a full and complete copy-paste of the error output when building. – Some programmer dude Mar 12 '20 at 17:50
  • 1
    You're using circular includes. See [this question](https://stackoverflow.com/questions/5319906/error-expected-class-name-before-token). – 1201ProgramAlarm Mar 12 '20 at 17:54
  • Making a forward declaration [like this](https://godbolt.org/z/-ywDRd) should solve it as pointed out in the link @1201ProgramAlarm shared. – Ted Lyngmo Mar 12 '20 at 18:00
  • 1
    Thanks to all of you, the "Forward declaration" solved the Problem. I really have to figure out why. @ Some programmer dude: thats the only error Output. – MasyM0 Mar 12 '20 at 21:53
  • @MasyM0 Just follow the includes. When the first includes the second, you'll notice that the second header depends on the class in the first. Catch 22. – Ted Lyngmo Mar 13 '20 at 15:49

0 Answers0