0

I have one class, called A, and it has it's own header file. Then I have another class, called B, which also has it's own header file. They each have their own .cpp file where I implement all of their functions.

I'm trying to have class B have a variable of class type A as a private variable, but I keep getting the error 'A' does not name a type

My code looks like this:

main.h:

#ifndef MAIN_H #define MAIN_H #include "A.h" #include "B.h" #endif 

main.cpp:

#include "main.h" int main( int argc, char* args[]) { B test; } 

A.h:

#ifndef A_H #define A_H #include "main.h" class A { public: //public functions private: //private variables }; #endif 

B.h:

#ifndef B_H #define B_H #include "main.h" class B { public: //public functions... private: A temp; } #endif 

So all of my includes are in main.h, which includes A before B. B has a variable of type A, but it is included from being in main.h and B.h includes main.h. However, I keep getting an error saying:

error: 'A' does not name a type. 

I've done some googling, and it seems like that means that A isn't defined when you use it, but it should be defined there since it's being included in main.h, right?

3
  • Why are you not just including B.h in main.cpp and A.h in B.h? Include only the things you need. I am curious if your compiler is getting confused by the include recursion. main.h -> A.h -> main.h, etc. Commented Feb 3, 2011 at 21:04
  • 1
    @Mark Loeser: The guard constants (xx_H) would avoid recursion problems, however the include policy is, indeed, very strange. Commented Feb 3, 2011 at 21:05
  • @trojanfoe: Yea...Its been a very long day. I really should go home :) Commented Feb 3, 2011 at 21:06

3 Answers 3

8

The problem is that A.h includes main.h, which includes B.h, which tries to use A.

The good way to organize your files would be this:

main.h:

// not needed 

main.cpp:

#include "B.h" // for using class B int main( int argc, char* args[]) { B test; } 

A.h:

#ifndef A_H #define A_H // no includes needed ATM class A { //... }; #endif 

B.h:

#ifndef B_H #define B_H #include "A.h" // for using class A class B { //public functions... } #endif 

That way, B.h is self-contained and can be used without having to include anything else before it. That's very important as soon as your project grows above the toy level it is at now. Why would anyone trying to use what header x.h provides need to know to also include f.h, m.h, and u.h?

Sign up to request clarification or add additional context in comments.

2 Comments

That makes sense, but the reason I had everything included inside of main.h and have everything include main.h was because otherwise I always have trouble including other stuff. I have a bunch of different classes all which need SDL.h, and there wasn't a problem including it before, but now that I changed how everything is included to your way, half of my files that need SDL.h say that all of the SDL functions in them are undefined, even though I'm including SDL.h at the top of that file.
Never mind, apparently the default settings to link SDL in codeblocks didn't work after I rearranged my code. I linked -lSDL and -lSDLmain directly instead of sdl-config and now it seems to compile. Thanks!
2

The code you provide compiles properly if you add a ; at the end of B.h

A better way of doing it would be #include "A.h" in "B.h", instead of #include "main.h"

But it is probably unrelated to your problem.

That kind of error may also be confusing if you are using templates and forget "typename".

Comments

0

A.h includes Main.h at the top.

Main.h skips A.h because A_H is already defined, then includes B.h.

B.h tries to make use of A, but A.h hasn't finished compiling yet so the type isn't defined.

1 Comment

The code posted actually works. It starts by including main.h, main.h includes A.h, so A is already declared when it is needed by B.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.