Class Design Problem

Does anyone know a way in which I can achieve this?  

Class A
{
    B *classb;

    void somefunc(void)
    {
       classb = new B(this);    
    }
};

Class B
{
    B(A &a);
};

I seem to have got myself into "a which came first" scenario....
LVL 1
cwguesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cwguesAuthor Commented:
Edited text of question.
0
rgoughCommented:
This compiles ok.  Your constructor B::B was private and had no body.

#include <iostream>

class A;

class B
{
public:
   B(A *a) {};
};

class A
{
   B *b;

   void func(void)
   {
      b = new B(this);
   }
};

void main(void);

void main()
{
   A a;
   B b(&a);
}
0
sureshkumarCommented:
you have to do forward declaration for the class B.Since you are trying to do declare object of B.
that is simple

class B;
Class A
{
    B *classb;

    void somefunc(void)
    {
       classb = new B(this);      
    }
};

Class B
{
    B(A &a);
};

for further queries welcome
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

KangaRooCommented:
You need to do something called forward declararion of class B; you are telling the compiler that a class named B will exist. It is allowed to use pointers and refrences to a forward declaraed class.

class B;
class A
{
    B *classb;
  public:
    void somefunc(void);
};

class B
{
  public: // putting this public is a good idea
    B(A &a);
};

void A::somefunc(void)
{
   classb = new B(this);      
}

0
kodali120699Commented:
class B; // Forward declaration

class A
{
    B *classb;
    // You can not define this function here as B class is not yet declared

    void A::somefunc(void);
};

class B
{
public: // Make the constructor public
    B(A &a){} // Define it here or outside the class
};


void A::somefunc(void)
{
  classb = new B(*this);
}

void main()
{
    A a;
    B b(a);
}
0
cwguesAuthor Commented:
Cheers lads/lasses....

I knew there would be a simple answer......
0
rgoughCommented:
cwgues,

I answered first, how come I don't get the points?
0
KangaRooCommented:
Maybe it wasn't completelely correct. The best answer was forward declararion of class B
On the other hand, it is more polite from the questioneer if he requests for clarification if a given answer seems to be incomplete.
Too bad the accepted answer isn't completely correct either ;)
0
rgoughCommented:
The code in my answer contains the forward declaration.  I provided the code that solved cwgues' problem plus pointed out two more issues that kept it from compiling.

You're right though, I guess I should have pointed out the forward declaration explicitly since it was the main point.  However the complete answer was there.
0
cwguesAuthor Commented:
I just needed to know how to do the forward declaration the other corrections are superficial  

You are all right, however sureshkumar was the first person to "propose the answer" I don't know if you can give points to a "Comment"... using this system..... I would if I could.....

If you really want some points I will construct a nonsense question for you to answer with equivalent points...  
If you want me to do this then say so by putting another comment here....  

I just want to get back to my coding...


0
KangaRooCommented:
It should be possible to grade a comment as an answer. The blue title bar of a comment is supposed to have a tiny link to the right, saying something like 'grade this comment' (I'm not sure). This is off course only possible (and visible) if a question hasn't been answered yet.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.