Skip to main content
GameDev.net gamedev.net
🔒 Locked

"invalid use of incomplete type" and "forward declaration"

Started by M-374 LX Feb 18, 2010 at 6:37 PM 1 replies 39.8k views
Original Post
M-374 LX
M-374 LX
These are the files: a.h:

#ifndef _A_
#define _A_

#include "b.h"

class A
{
public:
	B b;
	void test();
};

#endif
b.h:

#ifndef _B_
#define _B_

class A;

class B
{
public:
	A* parent;
	void test();
};

#endif
b.cpp:

#include "b.h"

void B::test()
{
	parent->test();
}
main.cpp:

#include <stdio.h>

#include "a.h"

void A::test()
{
	printf("Test.\n");
}

int main()
{
	return 0;
}
I try to compile, but I get errors:

g++ test.cpp b.cpp
b.cpp: In member function ‘void B::test()’:
b.cpp:5: error: invalid use of incomplete type ‘struct A’
b.h:4: error: forward declaration of ‘struct A’
Notice that I do not get any errors if I comment a part of b.cpp:

void B::test()
{
	//parent->test();
}
What causes the errors?
My current project: Lyjok's World, an open source, cross-platform horizontal shoot 'em up.
pablo24
pablo24
Include "a.h" in b.cpp
Zahlman
Zahlman
When the compiler attempts to compile b.cpp, b.cpp includes b.h, which provides the forward declaration 'class A;', but not the definition of the class (because that is in a.h, not in b.h). So the compiler knows that 'A' names a class, but it doesn't know that it has a member function called 'test', so it can't use that.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.