When apply observer pattern an error occured


When apply observer pattern an error occured



I have the following code:


class ISubscriber;
class News {
public:
float getVersion() { return this->version; }
void setVersion(float state) { this->version= state; this->notifyAllSubscribers(); }
void attach(ISubscriber *observer) { this->subscribers.push_back(observer); }
void notifyAllSubscribers() {
for (vector<ISubscriber*>::iterator it = subscribers.begin(); it != subscribers.end(); it++){
(*(*it)).update();
}
}
private:
vector<ISubscriber*> subscribers;
float version;
};

class ISubscriber {
public:
News *news;
virtual void update() = 0;
};

class Subscriber1 : public ISubscriber {
public:
Subscriber1(News *news) { this->news = news; this->news->attach(this); }
void update() override { cout << "Subscriber1: A new version of the newspaper has been launched (v" << this->news->getVersion() << ")" << endl; }

};

class Subscriber2 : public ISubscriber {
public:
Subscriber2(News *news) { this->news = news; this->news->attach(this); }
void update() override { cout << "Subscriber2: A new version of the newspaper has been launched (v" << this->news->getVersion() << ")" << endl; }

};


int main(int argc, char *argv) {
News newspaper;
newspaper.setVersion(2.1f);

Subscriber1 sb1(&newspaper);
Subscriber2 sb2(&newspaper);
return 0;
}



But strange errors happened:





The first error points to this code (*(*it)).update(); in news class.
Why that errors happened, what's the reason?


(*(*it)).update();


news




1 Answer
1



(*(*it)).update(); requires the type ISubscriber to be complete, just the forward declaration is not enough.


(*(*it)).update();


ISubscriber



You could move the defnition of ISubscriber before the one of News, and give a forward declaration of News before that.


ISubscriber


News


News


class News;
class ISubscriber {
public:
News *news;
virtual void update() = 0;
};

class News {
public:
float getVersion() { return this->version; }
void setVersion(float state) { this->version= state; this->notifyAllSubscribers(); }
void attach(ISubscriber *observer) { this->subscribers.push_back(observer); }
void notifyAllSubscribers() {
for (vector<ISubscriber*>::iterator it = subscribers.begin(); it != subscribers.end(); it++){
(*(*it)).update();
}
}
private:
vector<ISubscriber*> subscribers;
float version;
};





Thanks, But I am still don't understand why it did not work in my own way but it worked when I changed the position of the news class and ISubscriber (I want to understand the idea)?
– Lion King
Jun 30 at 17:40



news


ISubscriber





@LionKing (*(*it)).update(); is trying to call the member function update() on ISubscriber, before that the compiler needs to see the definition of ISubscriber, otherwise it can't know whether ISubscriber contains such a member function. Note the position, defining ISubscriber afterwards won't work.
– songyuanyao
Jul 1 at 2:04


(*(*it)).update();


update()


ISubscriber


ISubscriber


ISubscriber


ISubscriber






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

List of Kim Possible characters

Python Tkinter Error, “Too Early to Create Image”