-
September 28th, 2009, 02:40 AM
#1
Whats the problem with this code??
The following code is an implementation of Observer design pattern
Code:
#include<iostream>
#include<vector>
using namespace std;
class Subject{
vector<class Observer*> views;
int value;
public:
void attach(Observer *obs)
{
views.push_back(obs);
}
void setValue(int val)
{
value=val;
notify();
}
int getValue()
{
return value;
}
void notify();
};
void Subject::notify()
{
for(int i=0;i<views.size();i++)
{
views[i]->update();
}
}
class Observer{
Subject *model;
int denom;
public:
Observer(Subject *sub,int d):model(sub),denom(d)
{
model->attach(this);
}
virtual void update()=0;
protected:
Subject *getSubject()
{
return model;
}
int getDivisor()
{
return denom;
}
};
class DivObserver: public Observer{
public:
DivObserver(Subject *sub,int d):Observer(sub,d)
{
}
void update()
{
int v=getSubject()->getValue(),d=getDivisor();
cout << v << " div " << d << " is " << v / d << '\n';
}
};
class ModObserver: public Observer{
public:
ModObserver(Subject *sub,int d):Observer(sub,d)
{
}
void update()
{
int v=getSubject()->getValue(),d=getDivisor();
cout << v << " mod " << d << " is " << v % d << '\n';
}
};
int main()
{
Subject sub;
DivObserver d1(&sub,4);
DivObserver d2(&sub,3);
ModObserver m1(&sub,4);
sub.setValue(14);
cin.get();
return 0;
}
But Im getting the following error
error C2027: use of undefined type 'Observer'
-
September 28th, 2009, 02:48 AM
#2
Re: Whats the problem with this code??
Change
vector<class Observer*> views;
to
vector<Observer*> views;
Thanks for your help.
-
September 28th, 2009, 04:38 AM
#3
Re: Whats the problem with this code??
You also need a forward declaration of Observer before Subject´s class definition because it´s unknown to the Subject class.
Code:
...
class Observer;
...
class Subject
{
...
};
...
- Guido
-
September 28th, 2009, 04:56 AM
#4
Re: Whats the problem with this code??
Originally Posted by brett01
error C2027: use of undefined type 'Observer'
This code,
Code:
void Subject::notify()
{
for(int i=0;i<views.size();i++)
{
views[i]->update(); // Observer definition must be known
}
}
requires that the definition of Observer is know so move it to after the Observer definition.
That's the only change you have to do.
-
September 28th, 2009, 05:06 AM
#5
Re: Whats the problem with this code??
Originally Posted by Peter_APIIT
Change
vector<class Observer*> views;
to
vector<Observer*> views;
There's nothing wrong with that code.
Using the class keyword like that forward declares Observer at that particular place.
-
September 28th, 2009, 05:09 AM
#6
Re: Whats the problem with this code??
Originally Posted by GNiewerth
You also need a forward declaration of Observer before Subject´s class definition because it´s unknown to the Subject class.
A forward declaration cannot replace a class definition. In this case the definition of Observer must be known in the implementation of Subject's notify method
-
September 28th, 2009, 12:57 PM
#7
Re: Whats the problem with this code??
Originally Posted by nuzzle
A forward declaration cannot replace a class definition. In this case the definition of Observer must be known in the implementation of Subject's notify method
How to do that??
-
September 28th, 2009, 01:33 PM
#8
Re: Whats the problem with this code??
Originally Posted by brett01
How to do that??
A simple way is to define the classes (e.g., in header files) without defining their member functions, then define their member functions after both classes have been defined (e.g., in source files). If you really want to define the member functions inline with their class definitions, then pick one class and define all its member functions inline. For the other class, define inline all those member functions that do not need the definition of the first class. Now, you can place the second class' definition before the first class' definition, and define the second class' remaining member functions separately (e.g., in a source file).
-
September 28th, 2009, 04:29 PM
#9
Re: Whats the problem with this code??
Originally Posted by brett01
How to do that??
Well, I just told you in my reply #4.
-
September 28th, 2009, 06:52 PM
#10
Re: Whats the problem with this code??
Thank you very much nuzzle,I didnt notice that.
-
September 28th, 2009, 07:13 PM
#11
Re: Whats the problem with this code??
Originally Posted by brett01
Thank you very much nuzzle,I didnt notice that.
Well I first told you how to correct the problem and then I told the others why their suggestions were wrong.
Anyway it's usually better to organize code according to the traditional .h and .cpp system. But I also sometimes do as you did, putting everything on one file for ease of testing.
In both cases it's important to know whether a forward declaration is enough or if the full definition must be known. It determines the order of things.
Last edited by nuzzle; September 28th, 2009 at 07:20 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|