troubleshooting Question

Template, pointer initialization

Avatar of panJames
panJames asked on
C++
3 Comments3 Solutions307 ViewsLast Modified:
Hello experts,

please have a look at the code attached.

It does not compile...

Could you please tell me why?

Thank you

panJames

# include <iostream>
using namespace std;


class Animal
{
	int age;

	public:
		Animal() : age(10){};


		int AgeGet()
		{
			return age;

		}

};

template <class T>
class Zoo

{
	private:
		T * pObj; 
		int length;

	public:
		T & operator[] (int index)
		{
			return pObj[index];
		}

		Zoo() : length(12)
		{
			pObj = new T[12];  //container for Animals

			
			for (int i = 0; i < 12; i++)
			{
				pObj[i] = 0; //pointer initialization, it does not point at any object
				//xxxxxxxxxxxxxxxxxxxxx compiler crashes here xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

			}
		}


};

int main()
{
	Zoo<Animal> zoo; //it creates 12 spaces for objects

	Animal * animal = new Animal;


	cout << animal -> AgeGet();

	zoo[0] = *animal;


	delete animal;

	cout << zoo[0].AgeGet();





	return 0;

}
ASKER CERTIFIED SOLUTION
Orcbighter

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 3 Answers and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros