Enum type not declared

Hi, could someone help me figure out where the bug is in this code snippet? Compiler error is: 'A::type has not been declared". Below are the four files that should be relevant; the weird thing is that I have another example in my code that does almost the exact same thing but compiles just fine. The only difference between that example and this one is that the file dependencies are different; A does not need to call one of B's methods in the other example but it does in this one. Not sure if that's the reason.

Anyways, the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef A_H
#define A_H


//classA.h
#include "classB.h"
class A
{
public:
   enum type{type1, type2};
   type TYPE = type1;

   B* b;
   
   void callBfunc();
}

#endif //A_H


1
2
3
4
5
6
7
8

//classA.cpp
#include "classA.h"
void A::callBfunc()
{
   b->func(TYPE);
   return;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef B_H
#define B_H

//classB.h
#include "classA.h"

class B
{
public:
   void func(A::type TYPE);
}

#endif //B_H 


1
2
3
4
5
6
7
8
//classB.cpp
#include "classB.h"
void B::func(A::type TYPE)
{
   //do something
   return;
}




Last edited on

By default if you do not specify Public, Private or Protected in a class it will default to Private.
Thank you for your reply. It still does not compile as public.

Try this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//classA.h
#include "classB.h"
enum type{ type1, type2 };
class A
{
private:
	B* b;
	int TYPE = type1;
public:	
	void callBfunc();
};

//classA.cpp
#include "classA.h"
void A::callBfunc()
{
	b->func(TYPE);
	return;
}

//classB.h
class B
{
public:
	void func(int);
}

//classB.cpp
#include "classB.h"
void B::func(int type)
{
	//do something
	return;
}
I'll give that a shot. I'm still really curious why my way doesn't work, though.
You have recursive headers and no include guards.
a.h includes b.h. b.h includes a.h.

You have recursive headers and no include guards.
a.h includes b.h. b.h includes a.h.


Sorry, I actually do have include guards but the program still doesn't compile.
Last edited on
Actually, include guards are the issue.
In classA.h, remove your include line, swap it with a "class B;"
Include classB.h from classA.cpp.
Include guards by themselves don't solve the problem of recursive includes.
They simply prevent the compiler from looping forever reading header files

In a.h, remove line 6 (the include of b.h). In its place, put the following:

 
class B;  // forward declaration 


Now, b.h can include a.h without concern for recursive includes.

Topic archived. No new replies allowed.