class prototypes

This is a discussion on class prototypes within the C++ Programming forums, part of the General Programming Boards category; Why doesn't this compile: [edit]sorry got slightly ahead of myself Code: class b; class a { public: int m_a; a ...

  1. #1
    drt
    drt is offline
    Registered User
    Join Date
    Mar 2006
    Posts
    3

    class prototypes

    Why doesn't this compile:

    [edit]sorry got slightly ahead of myself

    Code:
    class b;
    
    class a
    {
    public:
    	int m_a;
    
    	a ()
    	{
    	};
    	a (b *bb)
    	{
    		m_a = bb->m_b;
    	};
    };
    
    class b
    {
    public:
    	int m_b;
    
    	b ()
    	{
    	};
    	b (a *aa)
    	{
    		m_b = aa->m_a;
    	};
    };
    Last edited by drt; 03-29-2006 at 02:37 PM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Code:
    a (b *bb)
    	{
    		m_a = bb->m_b;
    	};
    A forward reference doesn't let you declare an object of the class (or in your case a pointer to an object), it only allows you to refer to the class name. Why? Because nowhere in your forward reference does it say b contains a member m_b. The compile needs to know that.

    If you want to create an object of b, you have to define this function after you define the b class.
    Last edited by SlyMaelstrom; 03-29-2006 at 02:52 PM.
    Sent from my iPadŽ

  3. #3
    drt
    drt is offline
    Registered User
    Join Date
    Mar 2006
    Posts
    3
    Then how would you create a set of classes that are distinct but should be able to construct each other?

    Example: a Euler angle and quaternion class.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Quote Originally Posted by drt
    Then how would you create a set of classes that are distinct but should be able to construct each other?

    Example: a Euler angle and quaternion class.
    Are you serious? Is that the best example you can give?

    Anyway, as I was saying, you can have classes construct each other, but you have to define all of the classes before you do any of the implementation. Using your example above:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class b;
    
    class a {
       friend class b;  // Why? Because the data memebers should be private
       public:
          a (int val = 0) : m_a(val) {};
          a (const b&); // We'll define this outside of the class
          
          int getVal() { return m_a; }
       private:
          int m_a;
    };
    
    class b {
       friend class a;
       public:
          b (int val = 0) : m_b(val) {};
          b (const a&);
          
          int getVal() { return m_b; }
       private:
          int m_b;
    };
    
    // Now we implement the overloaded constructors
    
    a::a (const b &bb) {
       m_a = bb.m_b;
    };
    	
    b::b (const a &aa) {
       m_b = aa.m_a;
    };
    
    int main() {
        a aObj1(5);
        b bObj1(aObj1);
        a aObj2(bObj1);
    
        cout << bObj1.getVal() << endl << aObj1.getVal();
        
        cin.get();
        
        return 0;
    }
    Sent from my iPadŽ

  5. #5
    drt
    drt is offline
    Registered User
    Join Date
    Mar 2006
    Posts
    3
    I tried to think of a better example, but imagination failed me.

    This seems to work anyway, thanks.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    I think you can declare a pointer to an object of a class that has only been forward declared, but can't use operator->() on it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21