Q:

class prototypes not being read

im learing c++.I was goofing around trying new stuff and wanted to use an object class sub in  class object.But i was getting error saying that the object of class sub not in not defined.I know how to solve this issue i just have to move class sub above class object so that the compiler knows that there is a class called sub.
But I feel like this will get annoying as my code grows bigger and bigger so i tried forward declaring class like we do for function prototyping.But this doesn't work as it gives me this error -
'object::thing' uses undefined class 'sub'
Here is the code -
#include <iostream>
#include <vector>

class sub;
class object;

class object
{
private:
    sub thing;
    int ray;
public:
    void set(int n);
    void get() const;
};

class sub
{
public:
    int num;
public:
    void set_num(int n);
    void get_num() const;
};

int main()
{
    object ray;
    ray.set(4);
    ray.get();
}

Can you guys help me out??
thanks

A:

When a type is used in c++, at a minimum that type needs to have been declared previously.
However, in some cases, the type needs to be defined (complete) as well.
class A;

class B { 
  A a;    // error, because A needs to be defined   
}; 

In other cases, the type only needs to be declared (i.e. it can be incomplete):
class A;

class B { 
  A *a;    // fine, because A only needs to be declared   
}; 

If a type needs to be defined when it's used (as in your case), then you have no choice but to define it before hand. You could put the definition in a header file, but that file still needs to be included before the type is used.

