0

Hello all:

I am working on a class for creating 2-D arrays.

A couple of days ago, someone kindly helped me identify a problem with my operator() expression, but now I'm getting a segmentation fault. I just can't figure out where the problem is!

If anyone could take a look at my code and let me know about any problems, I would be very grateful.

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();
// Various functions...
T& operator() ( int x, int y );
const T& operator() ( int x, int y ) const;
};
#endif
```

Definitions:

```
#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>::operator() ( int x, int y)
{
if( x >= rows || y >= cols || x < 0 || y < 0)
{
cout << "Problem with dimensions ";
return matrix[0][0];
}
else
{
return matrix[x][y];
}
}
template <typename T>
const T& Array2d<T>::operator()( int x, int y) const
{
if( x >= rows || y >= cols || x < 0 || y < 0)
{
cout << "Problem with dimensions ";
return matrix[0][0];
}
else
{
return matrix[x][y];
}
}
template class Array2d<int>;
template class Array2d<double>;
template class Array2d<long>;
```

*Edited 6 Years Ago by RobBobSmith*: n/a