[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

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....
0
cwgues
Asked:
cwgues
  • 3
  • 3
  • 3
  • +2
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now