This is my program iam just new to c++ and linux
#include<iostream>
#include<istream>
#include<ostream>
// A abstract class for 1D array
template<class T>
class Array1D
{
public:
Array1D( int size=0); // this the default constructor
Array1D( const Array1D<T>& v);//this is copy constructor for so that we can intialise one array object with another
~Array1D() { delete [] element;}
T& operator[] (int i) const;
int Size() { return size;}
void Resize(int f){int c; delete [] element; size=c; element=new T [size];}
Array1D<T>& operator=(const Array1D<T>& v); //this function returns a this pointer
Array1D<T>& operator+=(const T& x) ;
Array1D<T> operator +(const Array1D<T>& m)const;
private:
int size;
T *element; // for allocating the array
};
template<class T>
Array1D<T>::Array1D( int sz)
{
size=sz;
element=new T[sz];
}
template<class T>
Array1D<T>::Array1D(const Array1D<T>& v) //to intialise another array with this one
{ //this is a copy coonstructor definition
size=v.size;
element= new T[size];
for(int i=0;i<size;i++)
element[i]=v.element[i];
}
template<class T>
T& Array1D<T>:
perator[](int i)const
{
return element[i];
}
template<class T>
Array1D<T> & Array1D<T>:
perator =(const Array1D<T>& v) // function templates
{
if(this != &v)
{
size=v.size;
delete [] element;
element =new T[size];
for(int i=0;i<size;i++)
element[i]=v.element[i];
}
return *this; //to make the assignment possible
}
template<class T>
Array1D<T>& Array1D<T>:
perator+=(const T& x)
{
for(int i=0;i<size;i++)
element[i]+=x;
return *this;
}
template<class T>
Array1D<T> Array1D<T>:
perator +(const Array1D<T>& m)const
{
Array1D<int> w(size);
for(int i=0;i<size;i++)
w.element[i]=element[i]+m.element[i];
return w;
}
/*int main()
{
Array1D<int> a1(12);
a1[0]=1;
a1[2]=3;
a1+=1;
Array1D<int> a2=a1;
std:: cout<< a2[0];
std::cout<<std::endl;
Array1D<int> a3;
a3=a1;
std::cout<<a3[2];
return 0;
}*/
template<class T>
class Array2D
{
public:
Array2D(int r=0,int c=0);
Array2D(const Array2D<T>& m);
~Array2D() { delete [] rows;}
Array1D<T>& operator [](int i)const;
Array2D<T>& operator = (const Array2D<T>& v);
Array2D<T> operator + (const Array2D<T>& m)const;
Array2D<T> operator - ()const;
Array2D<T>& operator += (const T& x);
private:
int row,col;
Array1D<T> *rows;
};
template<class T>
Array2D<T>::Array2D(int r,int c)
{
row=r;
col=c;
rows=new Array1D<T> [r];
for(int i=0;i<r;i++)
rows[i].Resize(c); // Resize is a new function in the Array1D class
}
template<class T>
Array2D<T>::Array2D(const Array2D<T>& m)
{
row=m.row;
col=m.col;
rows=new Array1D<T> [row];
for(int i=0;i<row;i++)
rows[i]=m.rows[i];
}
template<class T>
Array2D<T>& Array2D<T>:
perator = (const Array2D<T>& v)
{
if(*this!=&v)
{
row=v.row;
col=v.col;
rows=new Array1D<T> [row];
for(int i=0;i<row;i++)
rows[i]=v.rows[i];
}
return *this;
}
template<class T>
Array1D<T>& Array2D<T>:
perator [] (int i) const
{
return rows[i];
}
template<class T>
Array2D<T> Array2D<T>:
perator + (const Array2D<T>& m) const //remember *this is returned
//used only while
{
Array2D<T> w(row,col);
for(int i=0;i<row;i++)
w.rows[i]=rows[i]+m.rows[i];
return w;
}
template<class T>
Array2D<T>& Array2D<T>:
perator += (const T& x)
{
for(int i=0;i<row;i++)
rows[i]=rows[i]+x;
return *this;
}
template<class T>
Array2D<T> Array2D<T>:
perator - ()const
{
Array2D<T> w(row,col);
for(int i=0;i<row;i++)
w.rows[i]= -rows[i];
return w;
}
#include<iostream>
#include "1.h"
#include "2.h"
int main()
{
Array2D<int> a1(2,2);
a1[1][2]=0;
a1[1][1]=1;
a1[2][1]=1;
std::cout<<a1[1][1];
std::cout<<std::endl;
std::cout<<std::endl; //so how to do a2=-a1//
Array2D<int> a2(a1);
return 0
}
when i run this program on g++ compiler of linux fedora
i get a segmentation error
it seems to compile fine but when i run the program it gives the error
when irun the program on borland it runs but after that it gives null pointer assignment
please tell me exactly where i am wrong in may be the assignment
operator function of the Array2D
iam very new to c++ so may be objects not properly used with the arrays
please help me as quickly as possible
thank you