Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6357733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:13:31+00:00 2026-05-24T23:13:31+00:00

I am trying to implement an observer pattern with a template subject class. The

  • 0

I am trying to implement an observer pattern with a template subject class. The observers don’t (need to) know the subjects type, so I made an interface for the attach method without this type. This is my implementation:

SubjectInterface.h

#ifndef SUBJECTINTERFACE_H_
#define SUBJECTINTERFACE_H_
#include <list>
#include "Observer.h"
// Template-independant interface for registering observers
class SubjectInterface
{
public:
  virtual void Attach(Observer*) = 0;
}; // class SubjectInterface
#endif // SUBJECTINTERFACE_H_

Subject.h

#ifndef SUBJECT_H_
#define SUBJECT_H_
#include <list>
#include "Observer.h"
#include "SubjectInterface.h"
template <class T>
class Subject : public SubjectInterface
{
public:
  Subject();
  ~Subject();
  void Attach(Observer*);
private:
  T                        mValue;
  std::list<Observer*> mObservers;
}; // class Subject
#include "Subject.cpp"
#endif // SUBJECT_H_

Subject.cpp

template <class T>
Subject<T>::Subject()
{
}
template <class T>
Subject<T>::~Subject()
{
}
template <class T>
void Subject<T>::Attach(Observer* test)
{
  mObservers.push_back(test);
}

Observer.h

#ifndef OBSERVER_H_
#define OBSERVER_H_
#include "SubjectInterface.h"
#include <iostream>
class Observer
{
public:
  Observer(SubjectInterface* Master);
  virtual ~Observer();
private:
  SubjectInterface* mMaster;
}; // class Observer
#endif // OBSERVER_H_

Observer.cpp

#include "Observer.h" // include header file
Observer::Observer(SubjectInterface* Master)
{
  Master->Attach(this);
}
Observer::~Observer()
{
}

When I compile this using the gcc 4.3.4, I get the following error message:

SubjectInterface.h:10: error: ‘Observer’ has not been declared

I don’t understand this, because the Observer is included just a few lines above. When I change the pointer type from Observer* to int*, it compiles OK. I assume that there is a problem with the template subject and the non-template interface to it, but that is not what gcc is telling me and that doesn’t seem to be the problem when using int*.

I searched for template/observer, but what I found (e.g. Implementing a Subject/Observer pattern with templates) is not quite what I need.

Can anyone tell me, what I did wrong or how I can call the templated attach-method from a non-template observer?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:13:32+00:00Added an answer on May 24, 2026 at 11:13 pm

    You have a circular include chain, SubjectInterface.h includes Observer.h which in turns includes SubjectInterface.h.

    This means that the include guards will prevent Observer from being visible. To fix it instead forward declare Observer.

    // SubjectInterface.h
    #ifndef SUBJECTINTERFACE_H_
    #define SUBJECTINTERFACE_H_
    #include <list>
    class Observer; //Forward declaration
    // Template-independant interface for registering observers
    class SubjectInterface
    {
    public:
      virtual void Attach(Observer*) = 0;
    }; // class SubjectInterface
    #endif // SUBJECTINTERFACE_H_
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement observer pattern for my project. The LeaveFields class extends
I'm primarily an Objective-C/Cocoa developer, but I'm trying to implement the Observer pattern in
I'm trying to implement an Observer Pattern suggested here; Observer pattern in Go language
I have two questions: I'm trying to implement an Observer design pattern in Python.
I have a order beverage type program. I am tryig to implement observer pattern
I'm trying to implement an MVP pattern using STL and I have used *shared_ptr*
Trying to implement Piwik using REST API over http but need a little help.
All I am currently trying implement something along the lines of dim l_stuff as
trying to implement a dialog-box style behaviour using a separate div section with all
Trying to implement a rating system of users and postings. What is the best

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.