Hi everyone:
I have been trying to make a class for creating 2-D dynamic arrays. I started off with using the "new" keyword, but had memory management problems. Someone suggested that it is easier and better to use vectors, so that's what I'm trying to do.
My problem is that when I run my code, I have a segmentation fault. I just cannot figure out where it is coming from. I would be very grateful if someone could take a look at my code and comment on any problems! Is it the operator(), maybe?
Header:
#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
#include <vector>
template <typename T>
class Array2d
{
private:
unsigned int rows, cols;
std::vector<std::vector<T> > matrix;
public:
// Constructors
Array2d( unsigned int rowsPub, unsigned int colsPub );
~Array2d();
// Functions (not included)
T& operator() ( int x, int y );
const T& operator() ( int x, int y ) const;
};
#endif
Cpp file:
#include "Array2d.h"
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <new>
using namespace std;
template <typename T>
Array2d<T>::Array2d(unsigned int rowsPub, unsigned int colsPub)
: matrix(rows, vector<T>(cols,0))
{
for(int i=0; i<rows; ++i)
{
matrix.push_back(std::vector<T>(cols));
}
}
template <typename T>
Array2d<T>::~Array2d()
{
}
// Functions (not included)...
template <typename T>
T& Array2d<T>:perator() ( int x, int y)
{
if( x >= rows || y >= cols || x < 0 || y < 0)
{
cout << "Dimensions problem ";
return matrix[0][0];
}
else
{
return matrix[x][y];
}
}
template <typename T>
const T& Array2d<T>:perator()( int x, int y) const
{
if( x >= rows || y >= cols || x < 0 || y < 0)
{
cout << "Dimensions problem ";
return matrix[0][0];
}
else
{
return matrix[x][y];
}
}
template class Array2d<int>;
template class Array2d<double>;
template class Array2d<long>;
I have been trying to make a class for creating 2-D dynamic arrays. I started off with using the "new" keyword, but had memory management problems. Someone suggested that it is easier and better to use vectors, so that's what I'm trying to do.
My problem is that when I run my code, I have a segmentation fault. I just cannot figure out where it is coming from. I would be very grateful if someone could take a look at my code and comment on any problems! Is it the operator(), maybe?
Header:
#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
#include <vector>
template <typename T>
class Array2d
{
private:
unsigned int rows, cols;
std::vector<std::vector<T> > matrix;
public:
// Constructors
Array2d( unsigned int rowsPub, unsigned int colsPub );
~Array2d();
// Functions (not included)
T& operator() ( int x, int y );
const T& operator() ( int x, int y ) const;
};
#endif
Cpp file:
#include "Array2d.h"
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <new>
using namespace std;
template <typename T>
Array2d<T>::Array2d(unsigned int rowsPub, unsigned int colsPub)
: matrix(rows, vector<T>(cols,0))
{
for(int i=0; i<rows; ++i)
{
matrix.push_back(std::vector<T>(cols));
}
}
template <typename T>
Array2d<T>::~Array2d()
{
}
// Functions (not included)...
template <typename T>
T& Array2d<T>:perator() ( int x, int y)
{
if( x >= rows || y >= cols || x < 0 || y < 0)
{
cout << "Dimensions problem ";
return matrix[0][0];
}
else
{
return matrix[x][y];
}
}
template <typename T>
const T& Array2d<T>:perator()( int x, int y) const
{
if( x >= rows || y >= cols || x < 0 || y < 0)
{
cout << "Dimensions problem ";
return matrix[0][0];
}
else
{
return matrix[x][y];
}
}
template class Array2d<int>;
template class Array2d<double>;
template class Array2d<long>;