LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > LinuxQuestions.org > LQ Suggestions & Feedback
User Name
Password
LQ Suggestions & Feedback Do you have a suggestion for this site or an idea that will make the site better? This forum is for you.
PLEASE READ THIS FORUM - Information and status updates will also be posted here.

Notices


Reply
  Search this Thread
Old 04-30-2004, 11:21 AM   #1
ajay_linux
LQ Newbie
 
Registered: Apr 2004
Location: PUNE
Distribution: fedora
Posts: 1

Rep: Reputation: 0
Red face segmentation error c++ arrays with objects


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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
error code for pointers and 2-Dimensional arrays shams Programming 1 08-06-2004 10:00 PM
segmentation error harbir Programming 5 07-26-2004 03:10 PM
java arrays and objects titanium_geek Programming 13 03-07-2004 01:21 PM
Segmentation error - WHAT??????????? the_rydster Linux - Laptop and Netbook 1 11-16-2003 05:40 PM
arrays of pointers to class objects tdurden Programming 5 12-16-2002 10:47 AM

LinuxQuestions.org > Forums > LinuxQuestions.org > LQ Suggestions & Feedback

All times are GMT -5. The time now is 06:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration