?
Solved

Forward declaration and usage

Posted on 2005-04-06
11
Medium Priority
?
330 Views
Last Modified: 2010-04-01
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?
0
Comment
Question by:moshem
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 1200 total points
ID: 13716283
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
 
LVL 22

Expert Comment

by:grg99
ID: 13716292
Don't put the code body up there, move it to after the declaration for class B.

0
 
LVL 1

Author Comment

by:moshem
ID: 13716370
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 48

Expert Comment

by:AlexFM
ID: 13716442
Non-static class members are initialized in constructor:

class A
{
public:
    A();

    int n1;
    int* p;
};


A::A() : n1(10), p(null)
{
}
0
 
LVL 1

Author Comment

by:moshem
ID: 13716553
I know, thats exactly what I did and then recieved the above warning.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 13716623
Can you show your code which causes this message?

0
 
LVL 1

Author Comment

by:moshem
ID: 13716736
CClass base_price
{
      CClass  (data_struct * parent) {}
}

data_struct::data_struct () : base_price (this, 1), price (this, 2)
{
      bChanged = false;
}
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 13716992
I don't understand this code. What is CClass? What is data_struct? Please post enough code to understand what happens.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 13717068
>>>> 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
 
LVL 6

Expert Comment

by:SJT2003A
ID: 13717142
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 13718613
>>>> 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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question