CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Oct 2008
    Posts
    59

    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'

  2. #2
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Whats the problem with this code??

    Change
    vector<class Observer*> views;

    to
    vector<Observer*> views;
    Thanks for your help.

  3. #3
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    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

  4. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: Whats the problem with this code??

    Quote Originally Posted by brett01 View Post
    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.

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: Whats the problem with this code??

    Quote Originally Posted by Peter_APIIT View Post
    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.

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: Whats the problem with this code??

    Quote Originally Posted by GNiewerth View Post
    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

  7. #7
    Join Date
    Oct 2008
    Posts
    59

    Re: Whats the problem with this code??

    Quote Originally Posted by nuzzle View Post
    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??

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Whats the problem with this code??

    Quote 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).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    May 2009
    Posts
    2,413

    Re: Whats the problem with this code??

    Quote Originally Posted by brett01 View Post
    How to do that??
    Well, I just told you in my reply #4.

  10. #10
    Join Date
    Oct 2008
    Posts
    59

    Re: Whats the problem with this code??

    Thank you very much nuzzle,I didnt notice that.

  11. #11
    Join Date
    May 2009
    Posts
    2,413

    Re: Whats the problem with this code??

    Quote Originally Posted by brett01 View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured