Sign in to follow this  
Followers 0
jaberwacky

[Solved] C++ errors in a small oop test

8 posts in this topic

#1 ·  Posted (edited)

I don't know what I did to get this to work! :unsure::>

May have something to do with initializing the vectors?

#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

class AbstractObserver
{
    public:
        virtual void Update() const = 0;
        virtual ~AbstractObserver() {};
};

// ----------------------------------------------------------------------------

class ConcreteObserver1 : virtual public AbstractObserver
{
    public:
        void Update() const { cout << "ConcreteObserver1::Update()\n\n"; }
        ~ConcreteObserver1() { cout << "ConcreteObserver1::~ConcreteObserver1()\n\n"; };
};

// ----------------------------------------------------------------------------

class ConcreteObserver2 : virtual public AbstractObserver
{
    public:
        void Update() const { cout << "ConcreteObserver2::Update()\n\n"; }
        ~ConcreteObserver2() { cout << "ConcreteObserver2::~ConcreteObserver2()\n\n"; };
};

// ----------------------------------------------------------------------------

class ConcreteObserver3 : virtual public AbstractObserver
{
    public:
        void Update() const { cout << "ConcreteObserver3::Update()\n\n"; }
        ~ConcreteObserver3() { cout << "ConcreteObserver3::~ConcreteObserver3()\n\n"; };
};

// ----------------------------------------------------------------------------

class ConcreteObserver4 : virtual public AbstractObserver
{
    public:
        void Update() const { cout << "ConcreteObserver4::Update()\n\n"; }
        ~ConcreteObserver4() { cout << "ConcreteObserver4::~ConcreteObserver4()\n\n"; };
};

// ----------------------------------------------------------------------------

class ConcreteObserver5 : virtual public AbstractObserver
{
    public:
        void Update() const { cout << "ConcreteObserver5::Update()\n\n"; }
        ~ConcreteObserver5() { cout << "ConcreteObserver5::~ConcreteObserver5()\n\n"; };
};

// ----------------------------------------------------------------------------

class ConcreteObserver6 : virtual public AbstractObserver
{
    public:
        void Update() const { cout << "ConcreteObserver6::Update()\n\n"; }
        ~ConcreteObserver6() { cout << "ConcreteObserver6::~ConcreteObserver6()\n\n"; };
};

// ----------------------------------------------------------------------------

class AbstractSubject
{
    public:
        virtual void Register(AbstractObserver* const) = 0;
        virtual void Unregister(AbstractObserver* const) = 0;
        virtual void Notify() = 0;
        virtual ~AbstractSubject() {};
};

// ----------------------------------------------------------------------------

class ConcreteSubject : public AbstractSubject
{
    private:
        vector<AbstractObserver*> observers;

    public:
        void Register(AbstractObserver* const a_observer) { observers.push_back(a_observer); }

        void Unregister(AbstractObserver* const a_observer)
        {
            for(vector<AbstractObserver*>::iterator iter = observers.begin() ; iter != observers.end() ; ++iter)
            {
                if((*iter) == a_observer)
                {
                    // Wasssssuuuupppppp?
                    // Not much.
                }
            }
        }

        void Notify()
        {
            for(vector<AbstractObserver*>::const_iterator iter = observers.begin() ; iter != observers.end() ; ++iter)
            {
                (*iter)->Update();
            }
        }

        ConcreteSubject() : observers(0) {};

        ~ConcreteSubject()
        {
            observers.clear();
            cout << "ConcreteSubject::~ConcreteSubject()\n\n";
        }
};

// ----------------------------------------------------------------------------

int main()
{
    ConcreteSubject* const c_subject = new ConcreteSubject();

    ConcreteObserver1* const c_observer1 = new ConcreteObserver1();
    c_subject->Register(c_observer1);

    ConcreteObserver2* const c_observer2 = new ConcreteObserver2();
    c_subject->Register(c_observer2);

    ConcreteObserver3* const c_observer3 = new ConcreteObserver3();
    c_subject->Register(c_observer3);

    ConcreteObserver4* const c_observer4 = new ConcreteObserver4();
    c_subject->Register(c_observer4);

    ConcreteObserver5* const c_observer5 = new ConcreteObserver5();
    c_subject->Register(c_observer5);

    ConcreteObserver6* const c_observer6 = new ConcreteObserver6();
    c_subject->Register(c_observer6);

    c_subject->Notify();

    delete c_observer6;
    delete c_observer5;
    delete c_observer4;
    delete c_observer3;
    delete c_observer2;
    delete c_observer1;
    delete c_subject;

    return 0;
}

YES!

Edited by LaCastiglione

Share this post


Link to post
Share on other sites



I'm no C++ whiz, but isn't c_subject a pointer? And you're trying to access members with object notation, using a period? And don't you need to use pointer notation, ie 'c_subject->Register'?

Share this post


Link to post
Share on other sites

Go learn about pointers then come back to the problem again.

Share this post


Link to post
Share on other sites

Also learn the difference between references and values.

Share this post


Link to post
Share on other sites

I assume that you refer to:

cout << "Address of c_observe1 : " << &c_observe1 << "\n";

If so then I have those references to display the addresses of the 'concreteobserver' objects for debugging purposes.

Share this post


Link to post
Share on other sites

No. I was referring to your function parameters.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0