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>;

I'm not really an expert but just to give you some advice,cause I know seg faults really makes one just want to give up. You probably know but just check for where(normally in for-loop) tries to access or create elements outside of the range of the array.

Like is all your variables initialized to the correct size,not really sure how you initialized your row variable? Is it through the vector?

Templates with vectors not really my specialty still need some experience in that area.

Hope I could help you only a bit...

Thanks Phillamon... Yes, I also suspect that's where my problem is. I've experimented and can't get rid of the segmentation fault. Aargh!!

I was using dynamic arrays, but ran into an insurmountable memory management problem. So now I'm trying vectors!

have you maybe tried in you compiler:

gdb -tui your compiled executable file (ex. task)

It helps to detect segmentation faults

have you maybe tried in you compiler:

gdb -tui your compiled executable file (ex. task)

It helps to detect segmentation faults

I had not tried that. I have been wondering about how to detect segmentation faults. Thanks for the tip!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.