RUN TIME ERROR SEGMENTATION FAULT (CORE DUMPED)

i run this program on gcc compiler ,and i got run time error of segmentation
fault.
when i debugged it got segmentation error in the parametrised constructor
why this is so
please help\
code is written below!!!

#include<iostream>
#define SIZE 3
using namespace std;
template <class T>
class array{
T *p;
public:
array();
array(T *a){for(int i=0;i<SIZE;i++) p[i]=a[i];}
friend ostream& operator<<(ostream &dout,array &a)
{
cout<<"\n\n the array elements are : ";
for(int i=0;i<SIZE;i++) dout<<a.p[i]<<" ";
return dout;
}
};

template <class T>
array<T>::array()
{
p=new T[SIZE];
for(int i=0;i<SIZE;i++)
p[i]=0;
}

int main()
{
int x[SIZE]={10,5,6};
array <int> b;
b=x;
cout<<b;
float y[SIZE]={12.5,18.6,20.4};
array <float> c;
c=y;
cout<<c;
cout<<"\n\nEnd of program...............\n\n";
return 0;
}

#define SIZE 3

Don't. Ever. Do. That.
I know many profs teach stuff like that, but it's bullshit. With that you pointlessly limit yourself.

Aside from that, you got a member variable T* p in your class that you never initialize. So you are trying to access illegal memory regions here.
i have initialised p in default constructor above main function.

template <class T>
array<T>::array()
{
p=new T[SIZE];
for(int i=0;i<SIZE;i++)
p[i]=0;
}

But not in array<T>::array(T*). Nor do you have a destructor. And you haven't defined a copy constructor and assignment operator.
Instead of
#define SIZE 3

use

const int SIZE = 3;
Topic archived. No new replies allowed.