Forward declaration and usage

Hi,

Maybe my C++ is getting rusty, but can someone explain to me what am I doing wrong here.

I am trying to declare a type (class/struct) that accepts a pointer to a parent which contains variables of that type. I also need the type to access some variables or methods in the parent by passing "this".

class B;

class A
{
public:
      B * pB;

      A (B * p)
      {
            pB = p;
      }

      void A_use_B ()
      {
            pB->bb = 5;
      }

};

class B
{
public:
      A variable;
      int bb;

      B () : variable (this)
      {
      }
};

I am getting an error on the :

pB->bb = 5;

line, saying :

test.h(22) : error C2027: use of undefined type 'B' : see declaration of 'B'
test..h(22) : error C2227: left of '->bb' must point to class/struct/union


any idea?
LVL 1
moshemAsked:
Who is Participating?
 
AlexFMConnect With a Mentor Commented:
Move implementation to .cpp file:

// A.h
class B;

class A
{
public:
     B * pB;

     A (B * p);
     void A_use_B ();
};

// A.cpp
#include "A.h"
#include "B.h"

     A::A (B * p)
     {
          pB = p;
     }

     void A::A_use_B ()
     {
          pB->bb = 5;
     }
0
 
grg99Commented:
Don't put the code body up there, move it to after the declaration for class B.

0
 
moshemAuthor Commented:
thanks,

one more thing, I am try to initialize the variable with "this"

class B
{
public:
     A variable;


I can't seem to declare it like " A variable (this)"

but when I initialize it in the initializer list I get an warning :

warning C4355: 'this' : used in base member initializer list


what is the optimal way to initialize these variables, I will need to initialize about 20 of them.

thanks
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
AlexFMCommented:
Non-static class members are initialized in constructor:

class A
{
public:
    A();

    int n1;
    int* p;
};


A::A() : n1(10), p(null)
{
}
0
 
moshemAuthor Commented:
I know, thats exactly what I did and then recieved the above warning.
0
 
AlexFMCommented:
Can you show your code which causes this message?

0
 
moshemAuthor Commented:
CClass base_price
{
      CClass  (data_struct * parent) {}
}

data_struct::data_struct () : base_price (this, 1), price (this, 2)
{
      bChanged = false;
}
0
 
AlexFMCommented:
I don't understand this code. What is CClass? What is data_struct? Please post enough code to understand what happens.
0
 
itsmeandnobodyelseCommented:
>>>> test.h(22) : error C2027: use of undefined type 'B' : see declaration of 'B'

You also can make the implementation in header file by making it inline:

class A
{
public:
     B * pB;

     A (B * p)
     {
          pB = p;
     }

     void A_use_B ();

};

class B
{
public:
     A variable;
     int bb;

     B () : variable (this)
     {
     }
};

inline
void A::A_use_B ()
{
      pB->bb = 5;
}

>>>> CClass base_price

???? Is base_price a class or a class member?

>>>> C4355

It's only a level-4 warning that comes because 'this' isn't well-defined in a initialiser list of a base class (actually at a time *before* the object was fully created). You may get rid of the message if you set parent to NULL in the intializer list and set the this pointer using a 'setParent(this)' in the body of the constructor.

Regards, Alex

0
 
SJT2003ACommented:
OK, let me be clear what exactly your requirement is. As per the last post,  you are calling the base constructor with this parameter.
I guess you already have a clear understanding of what 'this' is. this is a pointer which points to the currently being invoked/defined object.
I assume 'base_price'  and 'price' are plan data members (like int, flot, etc) and are able to hold one value at one.
So how can you initialize such kind of plain variables with two values. Something phishy.

Its better you please provide more details or a code fragment which has all the details of these two classes.

Good luck :)
0
 
itsmeandnobodyelseCommented:
>>>> I assume 'base_price'  and 'price' are plan data members (like int, flot, etc)

No, obviously price and best_price are class members of class data_struct . The class of the data members - I suppose it is called CClass (what a name!!) - has a constructor similar to

  CClass::CClass(data_struct* parent, int init);

The reason of the warning simply is, that 'this' shouldn't be used int initializer list as 'this' is available only in the body of the constructor or any other member function. If I am right, the code should be like the following:

class CClass
{
private:
     data_struct * par;
     int     price;
public:
      CClass (data_struct * parent, int init) : par(parent), price(init) {}
      void setParent(data_struct * parent) { par = parent; }
};

data_struct::data_struct () : base_price (NULL, 1), price (NULL, 2)
{
     base_price.setParent(this);
     price.setParent(this);
     bChanged = false;
}


Regards, Alex

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.