View Full Version: Problem with C++ Classes

C++ Learning Community > C++ Help > Problem with C++ Classes


Title: Problem with C++ Classes


kasun04 - January 23, 2007 04:13 AM (GMT)
I have 2 class call A and B.

Class A would call a method in class B and class B would call a method in class A.
To acheive this goal I created the interfaces in following manner.

CODE
class A
{
   private:
        B* pB;
    //-------rest
    public:
        void Register_B (B* _pPassedB); //this would assign pB in class A
        void TransferData();
}

class B
{
   private:
       A* pA
   
   public SendData(); //This one would call pA->TransferData()
}


---
I have included
"A.h" in B
as well as "B.h" in A

-------
but when I compiled this program I got an error saying that
--error: ISO C++ forbids declaration of 'A' with no type
---error: 'GWMain' has not been declared

Is there any fault with my design?????

Shonoby - January 23, 2007 05:27 AM (GMT)
Well, for starters the code:
CODE

class A
{
private:
B* pB;
//-------rest
public:
void Register_B (B* _pPassedB); //this would assign pB in class A
void TransferData();
}

class B
{
private:
A* pA

public SendData(); //This one would call pA->TransferData()
}

might look right but its not. First of all 'B* pB' was not declared correctly because of a misconception in having a class use another class. What you should have done is:
Class A
CODE

class B;  // forward declaration

class A
{
private:
B* pB;
//-------rest
public:
void Register_B (B* _pPassedB); //this would assign pB in class A
void TransferData();
}


Class B
CODE

class B;  // forward declaration

class B
{
private:
A* pA

public SendData(); //This one would call pA->TransferData()
}

Another error i see it that u have not values for 'A*' nor 'B*'. If u were trying to reference a class that would not work.
As you see that i used ' class B; // forward declaration '. This is done because we are not allowed to assign values to class data members in header files. So what you should do because of this, you have to reference, that class in the constructor like so:
CODE

class B; // forward deceleration

class A {
public:
A( B & )
....
};

The forward declaration is done so we reference an object in that class w/o having to use a header file.
You also missed the class constructor which is a must, also you need a destructor which is need to use ' termination housekeeping '. Which is complicated ( i have only learned a little brief intro to it so iam not 100% up on it ). But for the rest of the reason that your code was incorrect, you will need some1 else to explain it because i have not learned more into this subject of class. So srry this is all i can do for u....

P.S. To use a code block you have to use the key words:
[-C-O-D-E-] ... [-/-C-O-D-E-] ( w/o the '-' just used it so it would not display a code )
( the ... means the code ).

kasun04 - January 23, 2007 05:33 AM (GMT)
Dear Shonoby,

Thanks a lot to ur immediate reply...
This is quite helful.
I've fixed the problem. And now it's working properly

Shonoby - January 23, 2007 05:36 AM (GMT)
LOL, ohh i was not to sure that this would work but iam happy to see that it did. I really made it in a rush so, i was not positive if it would work. But u also fortified my theory on a class having a class, so thank you also. LOL

[EDIT] I did know that this would work, and the problem but what i really meant was that u helped me stand a firmer grip on my theory.




Hosted for free by InvisionFree