classes

Started by
1 comment, last by Kamataat 20 years, 8 months ago
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?
Advertisement
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.
You can''t. The class would be infinitely recursive. Perhaps you could store a child* instead?

This topic is closed to new replies.

Advertisement