0

my homework assignment is asking me to create a array2d class and I am having trouble compiling it. It crashes every time it is compiled and I am unsure what I am doing wrong. My debugger is saying it is during my set value portion but I am not sure what it is exactly. Help would be great!

#include <iostream>

using namespace std;

class array2D {
private:
    int xRes, yRes;
    float **xtable;
public:
    array2D(int xRes, int yRes){
        float **xtable;
        xtable = new float*[yRes];
            for(int i=0;i < yRes;i++) {
                xtable[i] = new float[xRes];}}
    ~array2D(){
        for (int i = 0; i<yRes; i++){
            delete [] xtable[i];}
        delete [] xtable;}
    void getSize(int &xRes, int &yRes){}
    int getValue (int x, int y){return xtable[x][y];}
    void setValue(int x, int y, int Val) {xtable[x][y]=Val;}
};

int main() {
    array2D *a = new array2D(320,240);
    int xRes, yRes;
    a->getSize(xRes,yRes);
    for(int i=0;i < yRes;i++){
        for(int j=0;j < xRes;j++){
            a->setValue(i,j,100); // constant value of 100 at all locations
        }
    }
    for(int i=0;i < yRes;i++){
       for(int j=0;j < xRes;j++){
           cout << a->getValue(i,j) << " ";
       }
       cout << endl;
    }
    delete a;
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
apples123
  • 13
  • 3
  • Doesn't `void getSize(int &xRes, int &yRes){}` need to be `void getSize(int &xRes, int &yRes){ xRes = this.xRes; yRes = this.yRes;}`? And indent your code properly so that it is more readable. – Spikatrix Jun 14 '15 at 06:02
  • sorry about the indentation, will keep that in mind. This was my first post. I am not sure by what you mean by this.xRes since I didn't make an object yet. – apples123 Jun 14 '15 at 06:10
  • You mean the compilation fails, not that the compiler crashed. – Ophir Gvirtzer Jun 14 '15 at 13:04

1 Answers1

3

In these lines

array2D(int xRes, int yRes){
    float **xtable;

you are declaring a local variable. The class member variable of the same name remains uninitialized and you use that later.

Remove the second line.

Also, the member variables xRes and yRes are not initialized either.

Use:

array2D(int xResIn, int yResIn) : xRes(xResIn), yRes(yResIn) {
   xtable = new float*[yRes];
   for(int i=0;i < yRes;i++) {
      xtable[i] = new float[xRes];
   }
}

Also, change

void getSize(int &xRes, int &yRes){}

to

void getSize(int &xResOut, int &yResOut)
{
   xResOut = this->xRes;
   yResOut = this->yRes;
}

As you expand this class, keep in mind The Rule of Three and implement the copy constructor and copy assignment operator.

array2D(array2D const& copy) { ... }
array2D& operator=(array2D const& rhs) { ... }
Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • When I remove that line, it says that float **xtable is a private member, which makes sense. Should I have an initization step somewhere before I start anything? – apples123 Jun 14 '15 at 06:08
  • @digon1631, that doesn't make sense. Which compiler are you using? – R Sahu Jun 14 '15 at 06:11
  • It actually is compiling now but crashing when it runs. My compiler is GNU GCC Compiler. – apples123 Jun 14 '15 at 06:15
  • Ahh I see it now. It compiles and works. I just failed to initialize the variables. Thanks! – apples123 Jun 14 '15 at 06:21
  • what does the "this-->xRes" portion mean? I understand that it is pointing to soemthing, but why use the word this? – apples123 Jun 14 '15 at 06:42
  • @digon1631, It's another syntax for accessing members of the current object. You may use `xResOut = xRes;` also. – R Sahu Jun 14 '15 at 07:07