"does not name a type" and "has incomplete type" errors.

Hello everyone,

I am writing a fairly simple program that animates a sprite using SDL, and cannot get past the errors "'CAnim' does not name a type" and "field 'Anim_Rock' has incomplete type"; although I have searched for an answer extensively on this forum and others, I'm still at a dead standstill on this. I don't get both errors at once, I get "incomplete type" when attempting a forward declaration fix.

To be more specific, here is my code... I am using Code::Blocks and MingW. I have cut out any SDL and have cut it down to the base problem, and nothing else. This code is, in its entirety, a separate program I wrote to attempt a debug, but to no avail.

CBase.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef CBASE_H_INCLUDED
#define CBASE_H_INCLUDED

#include "CAnim.h"

class CBase
{
    private:
        CAnim Anim_Rock;

    public:
        CBase();
};

#endif 


CAnim.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef CANIM_H_INCLUDED
#define CANIM_H_INCLUDED

#include "CBase.h"

class CAnim
{
    private:
        int TotalFrames;

    public:
        CAnim();
};

#endif 


CBase.cpp
1
2
3
4
5
6
#include "CBase.h"

CBase::CBase()
{

}


CAnim.cpp
1
2
3
4
5
6
#include "CAnim.h"

CAnim::CAnim()
{
    TotalFrames = 2;
}



A workaround I have already tried and failed is to affix a forward declaration to CBase.h, which is when I get the error "incomplete type":

1
2
3
4
5
6
7
8
9
#ifndef CBASE_H_INCLUDED
#define CBASE_H_INCLUDED

#include "CAnim.h"

class CAnim;

class CBase
//Prototypes and such here 



Ultimately, I have been able to get the program to run if I place both class declarations into the same header file (only if CAnim is before CBase, however). Forward declarations are not working for me, and I cannot keep it this way because it will create some problems as I introduce more classes into my program.

An answer would be greatly appreciated, as I have been dealing with this issue myself for a few frustrating hours; I have the annoying feeling that I'm making a very minor mistake.
It might be because you are including "CBase.h" and "CAnim.h" inside of each other
...
try this...

CAnim.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef CANIM_H_INCLUDED
#define CANIM_H_INCLUDED

// Took out the include here

class CAnim
{
    private:
        int TotalFrames;

    public:
        CAnim();
};

#endif  
Ah, thank you so much, it works fine now. I had no idea that including the header files into each other made for an error... I'm somewhat surprised I didn't find that before...
It isn't a problem with all includes in general, but just this situation. Before the compiler actually starts processing your program, it sifts through all of the macros. So an include statement to the compiler is as if the two files were one. and on line 9 in "CBase.h", it calls the "CAnim" class which is declared after.

No problem, good luck with C++!
Topic archived. No new replies allowed.