Archived

This topic is now archived and is closed to further replies.

classes

This topic is 5233 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How would I solve this problem?
#include <iostream>

using namespace std;

class base;

class child : public base
{
};

class base
{
	public:
		int a;
		child c;

		base() : a(10) {}
		~base() {}
};

int main()
{
	child c;

	cout << c.a << endl;

	system("PAUSE");

	return 0;
}
In this case, I get an error saying that class base has incomplete type. To solve this, I''d have to put base above child, but then I get an error saying that ''field c has incomplete type''. Forward declarations don''t help in either case. So which way can it be done?

Share this post


Link to post
Share on other sites
This works for me:


#include <iostream>
using namespace std;

class base
{
public:
int a;
//child c;

base() : a(10) {}
~base() {}
};

class child : public base
{
};



int main()
{
child c;
cout << c.a << endl;
system("PAUSE");
return 0;
}


I don''t think you can have an object of a derived class inside the base class but I may be wrong.

Share this post


Link to post
Share on other sites