Help with program structure

I haven't seen much about this in tutorials or in my books, so I'm pretty sure I'm doing it wrong (or at least, not the best way).

I have my Main.cpp, then I've put all of my classes and larger functions in individual header files. Then I have a last header file named "Includes.h", which is the only file I actually include in Main.cpp. It looks something like this:

Main.cpp:
#include "Includes.h"

Includes.h:
1
2
3
4
5
6
7
8
#include <iostream>
#include <ctime>
#include <vector>

// Prototypes from Main.cpp here

#include "SomeClass.h"
#include "ClassReliesOnSomeClass.h" 


In each header file I deleted everything that was generated and just treat it as a completely empty file. I don't know what that stuff does but apparently not much.

Here's a problem I've run into: In my current project I include SomeClass before including ClassReliesOnSomeClass, because, well, yeah. However, what do I do when I need SomeClass to rely on ClassReliesOnSomeClass?

In detail, I mean:

1
2
3
4
5
class Skill
{
    public:
        Actor GetActor(int i) const { return TheActor[i]; }
}


1
2
3
4
5
class Actor
{
    public:
        Skill GetSkill(int i) const { return TheSkill[i]; }
}


No matter which way around I include these classes, one will always be "not declared in this scope" because it hasn't been read yet.

How do I get around this, and structure my programs more efficiently? Thanks for the help.
Not sure what you're getting at but it sounds like this can be solved with some inheritance. Or just rethink the logic of this program
Just imagine you have a program with three classes in it, all in Main.cpp, typed one after the other. My problem is that I want a member function of the first class to use the type of class 3. I can't just switch them because class 3 also has member functions which require the type of class 1, and maybe class 2.

Inheritence could work. Would something like this solve the issue of being out of scope?:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Actor // Player
{
    // Class for player stats, known skills, held items etc.
    //...call parts of Skill and Ability
}

class Skill : public Actor
{
    // Class for reading skills from file, defining what they do etc.
    //...call parts of Actor and Ability
}

class Ability : public Actor
{
    // Class for reading abilities from file, defining what they do etc.
    //...call parts of Actor and Skill
}


If that would work then fine. I'm not really sure it makes much sense though, since Skill isn't a kind of Actor. I thought inheritance was for things like class Swan : public Bird? Should I used private inheritance?
Pre-declare the classes, ie:
1
2
3
4
5
6
7
class Actor;

class Skill
{
    public:
        Actor GetActor(int i) const { return TheActor[i]; }
};


1
2
3
4
5
6
class Skill;
class Actor
{
    public:
        Skill GetSkill(int i) const { return TheSkill[i]; }
};
Last edited on
Oh man, totally did not know you could prototype classes. Thanks a bunch!
Usually one declares classes in separate files and just includes them all into a main but doing it all in a signle file can cause error like that. In that case, you now know everything is read from the top of a file and that's that. If it isn't included at the top of the file it better have already been declared or it doesn't exist as far as the compiler is concerned.

Good luck.
Topic archived. No new replies allowed.