Class prototyping
Asked Answered
T

4

14

I have put several instances of class b in class a but this causes an error as class a does not know what class b is.

Now I know I can solve this problem by writing my file b a c but this messes up the reachability as well as annoys me. I know I can prototype my functions so I do not have this problem but have been able to find no material on how to prototype a class.

does anyone have an example of class prototyping in c++.

as there seems to be some confusion let me show you what i want

class A
{
public:

B foo[5];

};

class B
{
public:
int foo;
char bar;
}

but this does not work as A cannot see B so i need to put something before them both, if it was a function i would put A(); then implement it later. how can i do this with a class.

Triplett answered 31/5, 2011 at 13:2 Comment(3)
This question is very difficult to understand. What does writing my file b a c mean? What does prototyping a function mean?Significative
@Space_COwbOy writing my file b a c means writing class b then underneath it writing class a. this works but makes it hare to read as the information is nor presented in the order it make sense in.Triplett
4 years later, but why not just move B declaration at the beginning?Thayne
E
31

You can declare all your classes and then define them in any order, like so:

// Declare my classes
class A;
class B;
class C;

// Define my classes (any order will do)
class A { ... };
class B { ... };
class C { ... };
Entrammel answered 31/5, 2011 at 13:11 Comment(8)
they are in the same source fileTriplett
I thought by file b a c you meant 3 different files. The solution would be the same though, just declare class B before the definition of class A, then define class B. Alternatively, you can declare all classes before defining them in any order later on.Entrammel
I have class b defined before class a but I want it the other way around, I am asking how to declare my classes so I can define them in any order I want.Triplett
I see, I edited the answer to show you how to declare your classes and then define them.Entrammel
thank you so much, the problem was annoying and the answers even more so.Triplett
this did not solve the problem at all. my classes are inside a header file, and one class (called binary) is referencing the other 2 (called segments and relocation_symbols). although forward declaring the classes, class binary must stand at the very end of the file or else said error occurs. i mean it's totally not an issue that the ordering of classes is fixed. It just feels really uncomfortable, having the most important class not standing at the top (autism trigger :-P )Septilateral
Related read on forward declarations: SO: Error: Invalid Use of Incomplete Type. You can't reference class members of class B in the definition of class A.Hone
What about if we want to place main function right after these declarations and define the classes below? When I try doing that, I get a "Forward declaration error".Motherwell
R
5

You're looking for declarations.

class A;
class B {
    A MakeA();
    void ProcessA(A a);
};
class A {
    B bs[1000];
};

If you forward declare a class, you can

declare functions taking and returning it or complex types made of it
declare member variables of pointer or reference to it

This basically means that in any case which doesn't end up with instances of A inside B and vice versa, you should be able to declare and define any interface between A and B.

Rosenwald answered 31/5, 2011 at 13:15 Comment(1)
Its just one way like a team has many fighter classes and a fighter class has many move classes, but i think you solved my problemTriplett
E
2

The usual way to resolve circular dependencies is to use a forward declaration:

// Bar.h

class Foo; // declares the class Foo without defining it

class Bar {
    Foo & foo; // can only be used for reference or pointer
};

// Foo.h

#include <Bar.h>

class Foo {
    Bar bar; // has full declaration, can create instance
}

You can provide a full declaration and definition in another file. Using the forward declaration, you can create pointers and references to the class, but you cannot create instances of it, as this requires the full declaration.

Eyespot answered 31/5, 2011 at 13:9 Comment(0)
P
1

class b;

class a {
public:
     b * inst1;
};
class b{
....
};

Is this what you needed ?

Parnas answered 31/5, 2011 at 13:6 Comment(1)
yes exactly what I want but an instance not a pointer, is this possible ?Triplett

© 2022 - 2024 — McMap. All rights reserved.