How to pre-declarate a class like we do with functions?

We can pre-declarate functions like

1
2
3
void funtion (int a);
//...code...
void funtion (int a){...}


How do we do the same for classes? Or its not possible?

Last edited on
class A; // forward declaration of class A

What you can do with the class with just a forward declaration is very limited. You can have pointers and references to the class and not much more.
Ohh.. so you dont have the entire class and so :O Can i declarate them separately? No eh?

I have the problem that 2 classes CLIENT , CORPORATION are mutually linked in methods calls between them so I have the question "Which to put first??" :( :( :(

Is there anything to help me on this?
If they are in different .cpp/.h files:
#include "className.h"
Yes that what im doing but the there is one class CLIENT who restat the top of all and so

CORPORATINO from beneath cant call its methods

if i put include CLIENT at corporations too it says redefinition of course :(
Can we see your implementation. I can think of a few ways to go about this but some are more relavent then others depending on what specifically "links" the two classes together.
Eer... they:

*co-call methods one from the other

*and the VECTOR <* OFFICES>; is located to CORPORATION. But CLIENT needs of course to chose one from the Offices for his entrance

So the includes
#include "CLIENT.H"
#include "OFFICE.H"
#include "CORPORATION.H"

Work for CORPORATION ableing it to acess and create OFFICES and CLIENTS

BUT CLIENTS can't choose offices cause OFFICE type is unkwonw yet and cant even be passed in a CLIENT method to printout them at least. ^^
And of course CLIENT neither can call CORPORATION.Show_Offices()
Last edited on
Sorry I deleted my last post because I went off on some tangent. Anyway, You have three objects OFFICE, CLIENT and CORPORATION. OFFICE appears to be a list that is held in CORPORATION that you want CLIENT to be able to choose from. So the first thing is to do is make sure that the OFFICE list is a public data memeber and that Show_Offices() is also public so that CLIENT can reach them. It doesn't appear that CORPORATION needs to have CLIENT.H included but you will need OFFICE.H and CORPORATION.H included in CLIENT.H. Your CLIENT object needs to have a function that can except a CORPORATION object as an argument so that it can call the "Show_Offices()" member function. Is this possible so far?
Your CLIENT object needs to have a function that can except a CORPORATION object as an argument so that it can call the "Show_Offices()" member function. Is this possible so far?

Thats the thing cause cause I cant do
1
2
3
4
5
6
7
8
9
class CLIENT
{
public:
void Chose_Office(vector <*OFFICE>)
{
     OFFICE[i].showInfo();
     cin>> i_Chosen;
}
}


Sonce the office as a type is declared AFTER the CLIENT :(

P.S.
oh so Sory for the delay I went in bath
Last edited on
You should declare CLIENT last, I say this because nothing you've indicated so far tells me that either OFFICE or CORPORATION need to know anything about CLIENT. So CORPORATION.H should include OFFICE.H and CLIENT.H should include CORPORATION.H unless there is something preventing this.

Also that function should look more like this:
1
2
3
4
void CLIENT::Choose_Office(CORPORATION Cinc)
{
    Cinc.Name_Of_The_OFFICE_Variable_In_The_CORPORATION_Object[i]->Show_Office();
}

This is assuming that the list of OFFICE objects is unique to each instance of the CORPORATION object.

EDIT: Sorry I missed that it was a vector of pointers before, you'll need to dereference the vector entry to call the "Show_Office()" member function.
Last edited on
NO NO they do cause the CORPORATION is who adds clients and deletes :(

Sorry i thought this was clear and taken as granted globally

So there is no way that all clasees can access each other through a declaration like in function?

I have to restrain the accessibility? What wont I be able to do if i declare the class in advance as showed above? What are its limits

class A; // forward declaration of class A
If CORPORATION has both a list of CLIENT objects and OFFICE objects then the function should be implimented there.

1
2
3
4
void CORPORATION::Choose_Office(CLIENT Client, OFFICE Office)
{
    Client.Office = Office;
}


In the case I just posted CLIENT.H will include OFFICE.H and CORPORATE.H will include CLIENT.H.

There are still more options. I wanted to go straight to base pointers and virtual classes at first but I decided against it.
Thank you so thats the only easy solution except base pointers ans virtual classes

What about the limtits of forward classes declaration? What cant you do? U have only its adress and u cant call its methods as Peter87 sed?
I've yet to see "the only solution for X" in C++. But IMO this would be the easiest since the CORPORATION object has both lists in it's memory space.

I tend to avoid forward declarations so I wouldn't know all of the limitations. It makes sense though that the compiler would not know what functions it has.
Topic archived. No new replies allowed.