Thread: forward declaration error?

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    46

    forward declaration error?

    Code:
    class Bar;
    
    class Foo
    {
      public:
         Foo() { p_Bar = NULL;}
        ~Foo() {}
    
        void setBar(Bar *b) { p_Bar = b;}
        void doSomething()
        {
          std::cout<< "Hey I'm Foo and I'm doing something\n";
          notifyBar();
        }
      private:
        Bar *p_Bar;
        void notifyBar() {if (p_Bar != NULL) p_Bar->update();}
    
      protected:
    
    };
    
    
    class ControlFoo
    {
      public:
        ControlFoo() {}
        ~ControlFoo() {}
    
        void setFoo(Foo *f) {m_f = f;}
    
        void controlSomething() {m_f->doSomething();}
      private:
        Foo *m_f;
      protected:
    
    };
    
    
    class Bar
    {
      public:
        Bar() {}
        ~Bar() {}
        virtual void update() = 0;
        void setControlFoo(ControlFoo *cf) {m_cf = cf;}
      private:
    
      protected:
        ControlFoo *m_cf;
    };
    
    class BarBar: Bar
    {
      public:
        BarBar(): Bar() {}
        ~BarBar() {}
    
        virtual void update();
        void doBBSomething();
      private:
      protected:
    
    };
    
    void
    BarBar::update()
    {
      std::cout << "Hey! we got some update!\n";
    }
    
    void
    BarBar::doBBSomething()
    {
      m_cf->controlSomething();
    }
    The following example gives me the error
    t1.hpp: In member function ‘void Foo::notifyBar()’:
    t1.hpp:19: error: invalid use of incomplete type ‘struct Bar’
    t1.hpp:3: error: forward declaration of ‘struct Bar

    Can anyone help me understand this? Is this because Bar is not yet an complete type and the call in notifyBar requires it to be?

    Thanks!
    Last edited by sugarfree; 04-24-2010 at 11:21 PM.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I think that is the cause. The Bar::update function isn't defined

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    In the implementation of Foo::notifyBar() you are attempting to call p_Bar->update()

    Doing that requires the compiler to be able to see the definition of class Bar before that line. A forward declaration is not enough. Defining class Bar later in code is also not enough.

    The solution will be to move the implementation of Foo::notifyBar() out of the class, and to a point after both classes Foo and Bar are defined.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    Oh I see, the forward declaration allows Foo to use pointer or references to Bar but it doesn't allow any use of the Bar class members because they haven't been defined yet.

    Edit: I hadn't seen grumpy post yet. Thanks grumpy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM