Archived

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

Kamataat

classes

Recommended Posts

Kamataat    122
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
sheepsteak    122
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