I'm trying to implement an observer pattern who's subject monitors for uppercase letters in a line of text entered by the user. I havn't had much problem until now i'm getting the error "error C2243: 'type cast' : conversion from 'UpperCaseMonitor *' to 'Observable *' exists, but is inaccessible". I can't seem to figure out what the problem is. The error is somewhere in the update() method of the CountingObserver class. Could someone please help me out. Thank you.

#include <iostream>
#include <vector>
#include <list>
#include <cctype>
using namespace std;

class Observer;

//===========================================================================
class Observable
{
  public:
    virtual void attach(Observer*)  = 0;
    virtual void detach(Observer*)  = 0;
    virtual void notify()           = 0;

    // Can we get rid of these?
    virtual char get_state()     = 0;
    virtual void set_state(char) = 0;
};

class Observable;

//===========================================================================
class Observer
{
  public:
    virtual void update(Observable*) = 0;
};




class UpperCaseMonitor : Observable 
{
public:
	void attach(Observer*);  //Add the parameter to the list of objects that are observing this object
	void detach(Observer*); //Remove the parameter from the list of objects that are observing this object
    virtual void notify(); //Performs the notify-type behavior of the observer pattern
	
	virtual char get_state()    {return _state; } //Usual access-type behavior
	virtual void set_state(char new_state) {_state = new_state; notify();} //Usual mutator-type behavior
	void watch(char); //If the parameter is an uppercase character the object's state becomes the value of the parameter.

private:
	vector<Observer*> _observers;
	int _state;
};

class CountingObserver : Observer
{
public:
	CountingObserver(UpperCaseMonitor *);
	~CountingObserver();
	virtual void update(Observable*);

	void show_data();
//private:
	UpperCaseMonitor *_subject;
	int count;
};

/*class AccumulatingObserver : Observer
{
	void update(Observable*);

	void show_data();
};*/

int main(){
	UpperCaseMonitor uc_monitor;
	
	// Prompt
    cout << "Enter some text, type ^d when done.\n";

    // Process text
    char chr;
    while (cin.get(chr))
    {
        uc_monitor.watch(chr);
        
    }


}
void UpperCaseMonitor::attach(Observer* obs)
{
	_observers.push_back(obs);
}
void UpperCaseMonitor::detach(Observer* obs)
{
	int count = _observers.size();
	int i;

	for(i = 0; i < count; i++)
	{
		if(_observers[i] == obs)
		break;
	}
	if(i < count)
		_observers.erase(_observers.begin() + i);
}
void UpperCaseMonitor::notify()
{
	int count = _observers.size();

 for (int i = 0; i < count; i++)
   (_observers[i])->update(this); 
}
void UpperCaseMonitor::watch(char chr)
{
	if (isupper(chr))
	{
		set_state(chr);
	}
}
CountingObserver::CountingObserver(UpperCaseMonitor *s)
{
 _subject = s;
 _subject->attach(this);
}
CountingObserver::~CountingObserver()
{
_subject->detach(this);
}
void CountingObserver::update(Observable* ChangedState)
{
  if(ChangedState = _subject)
	  show_data();
}
void CountingObserver::show_data()
{
	int value = _subject->get_state();

	count += 1;

	cout << "Number of state changes is " << count << " and most recent state change is " << value << endl;
}

Recommended Answers

All 2 Replies

class UpperCaseMonitor : [B]public[/B] Observable
{
  // ...

Oh wow I don't know how i missed that. Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.