Link to home
Start Free TrialLog in
Avatar of moshem
moshem

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of grg99
grg99

Don't put the code body up there, move it to after the declaration for class B.

Avatar of moshem

ASKER

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
Non-static class members are initialized in constructor:

class A
{
public:
    A();

    int n1;
    int* p;
};


A::A() : n1(10), p(null)
{
}
Avatar of moshem

ASKER

I know, thats exactly what I did and then recieved the above warning.
Can you show your code which causes this message?

Avatar of moshem

ASKER

CClass base_price
{
      CClass  (data_struct * parent) {}
}

data_struct::data_struct () : base_price (this, 1), price (this, 2)
{
      bChanged = false;
}
I don't understand this code. What is CClass? What is data_struct? Please post enough code to understand what happens.
>>>> 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

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 :)
>>>> 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