1

I'm trying to make class functions I can tack on to other classes, like with nested classes. I'm still fairly new to C++, so I may not actually be trying to use nested classes, but to the best of my knowledge that's where I'm at.

Now, I've just written this in Chrome, so it has no real use, but I wanted to keep the code short.

I'm compiling on Windows 7, using Visual Studio 2015.

I have two classes in file_1.h:

#pragma once
#include "file_2.h"
class magic_beans{
public:
    magic_beans();
    ~magic_beans();

    int getTotal();
private:
    double total[2]; //they have magic fractions
}

class magic_box{
public:
    magic_box(); //initiate
    ~magic_box(); //make sure all objects have been executed

    void update();

    magic_beans beans; //works fine
    magic_apples apples; //does not work

private:
    int true_rand; //because it's magic
};

... And I have one class in file_2.h:

#pragma once
#include "file_1.h"
class magic_apples{
public:
    magic_apples();
    ~magic_apples();

    int getTotal();
private:
    double total[2];
}

Now, I've found that I can simply change:

magic_apples apples;

To:

class magic_apples *apples;

And in my constructor I add:

apples = new magic_apples;

And in my destructor, before you ask:

delete apples;

Why must I refer to a class defined in an external file using pointers, whereas one locally defined is fine?

Ideally I would like to be able to define magic_apples the same way I can define magic_beans. I'm not against using pointers but to keep my code fairly uniform I'm interested in finding an alternative definition method.

I have tried a few alternative defines of magic_apples within my magic_box class in file_1.h but I have been unable to get anything else to work.

Jarred
  • 67
  • 6
  • "Why must I refer to a class defined in an external file using pointers, whereas one locally defined is fine?" Who have told you that!?!? – Humam Helfawi Jan 22 '16 at 08:25
  • BTW you have circulation in includes without guard.. see this http://stackoverflow.com/questions/4767068/c-header-guards – Humam Helfawi Jan 22 '16 at 08:27
  • I haven't had any formal training using c++, only c, so I'm on my own here most of the time. I use #pragma once in all of my header files. – Jarred Jan 22 '16 at 08:28
  • it is ok but it's good to know that it is not standard way. Any way what about frist question? – Humam Helfawi Jan 22 '16 at 08:29
  • Nobody. I'm self-learning and that's the conclusion I have reached so far. I'm stuck and need help learning how to improve, so here I am. Eep, forward declarations and pointers... meaning I'm not doing enough with my headers? – Jarred Jan 22 '16 at 08:32
  • Wait, scratch that. I don't have any includes in file_2.h as it doesn't need any. I'll modify my code here. – Jarred Jan 22 '16 at 08:34

1 Answers1

3

You have a circular dependency, file_1.h depends on file_2.h which depends on file_1.h etc. No amount of header include guards or pragmas can solve that problem.

There are two ways of solving the problem, and one way is by using forward declarations and pointers. Pointers solve it because using a pointer you don't need a complete type.

The other way to solve it is to break the circular dependency. By looking at your structures that you show, it seems magic_apples doesn't need the magic_beans type, so you can break the circle by simply not includeing file_1.h. So file_2.h should look like

#pragma once
// Note no include file here!
class magic_apples{
public:
    magic_apples();
    ~magic_apples();

    int getTotal();
private:
    double total[2];
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Definitely, I rearranged my header files and the problem appeared elsewhere. Looks like I have a lot of work ahead of me to fix this one. I'll split and organise my headers more appropriately. Feels like a trivial question now, just once I'd like to ask a question and have it be a serious problem, but at the same time thank god it never is! – Jarred Jan 22 '16 at 08:43
  • After 45 minutes of moving #includes and prototypes around, I got it to compile as originally intended. Don't ignore it just because it works. Got it. – Jarred Jan 22 '16 at 09:35