Sign in to follow this  
junkacc

Forward declaring classes within namespaces (c++)

Recommended Posts

Hi, I'm using Visual Studio 2008 (c++) and I have been trying to forward declare a class within in its own namespace into another class which has its own namespace too, with very little success. This is a sample of what I'm trying to do:
namespace name1
{
	class class1;
}


namespace name2
{
	class class2
	{
	public:
		name1::class1 c1;
	};
}


With this sample code I get the compiler error: error C2079: 'name2::class2::c1' uses undefined class 'name1::class1 Can anyone help me figure out how to do this please? Or even if it is possible to do this?

Share this post


Link to post
Share on other sites
You can't use a forward declaration here because class2 is trying to hold class1 by value. Try switching to a (smart) pointer or reference.

Share this post


Link to post
Share on other sites
I was afraid that would be the answer I guess I'll just have to include the header. Thanks for the fast reply, it is much appreciated.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this