1

Below codes will show, when run, that the private member variables (MaxRows, MaxCols) are changing by the time function input is called. Could you please help what is going on?

As you can see the first constructor generates correct display of private variables. However the function will break them apart.

#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <windows.h>
#include <cstring>
#include <cctype>
#include <iomanip>
#include <algorithm>
#include <sstream>
using namespace std;

class TwoD
{
private:
    int MaxRows;
    int MaxCols;
    double** outerArray;

public:
    TwoD(int MaxRows, int MaxCols) 
    {
        outerArray = new double *[MaxRows];
        for (int i = 0; i < MaxRows; i++)
            outerArray[i] = new double[MaxCols];

            cout << MaxRows << MaxCols << endl;
    }

    void input()
    {
        cout << MaxRows << MaxCols << endl;
        for (int k = 0; k < MaxRows; k++)
        for (int j = 0; j < MaxCols; j++)
            cin >> outerArray[k][j];
    }

    void outPut()
    {
    for (int l = 0; l < MaxRows; l++)
    {
        for (int m = 0; m < MaxCols; m++)
            cout << outerArray[l][m] << " ";
        cout << endl;
    }
    }

    ~TwoD()
    {
    for (int i = 0; i < MaxRows; i++)
        delete[] outerArray[i];
    delete[] outerArray;
    }

};

int main()
{
TwoD example(5, 2);
example.input();
example.outPut();

return 0;
}
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
Ujae Kang
  • 157
  • 1
  • 8

2 Answers2

4

You never actually set the class members to the values passed as parameters.

Also: you might want to look at your naming convention - normally use maxRows not MaxRows

TwoD(int maxRows, int maxCols) : MaxRows(maxRows), MaxCols(maxCols)
    {
        outerArray = new ...
John3136
  • 28,809
  • 4
  • 51
  • 69
  • Thank you for your help.. I added ... TwoD(int a, int b) { outerArray = new double *[MaxRows]; for (int i = 0; i < MaxRows; i++) outerArray[i] = new double[MaxCols]; MaxRows = a; MaxCols = b; cout << MaxRows << MaxCols << endl; }..... However it creates another problem. The program won't even run. Did I do it correct? – Ujae Kang Sep 09 '13 at 00:30
  • Your snippet still doesn't address the issue: class members `MaxRows` and `MaxCols` are never set. – John3136 Sep 09 '13 at 00:34
  • But wouldn't "TwoD(int a, int b)" flow into "MaxRows = a; MaxCols = b;" in order to update the class member MaxRows and MaxCols? – Ujae Kang Sep 09 '13 at 00:38
  • I think I can close this case, since I got it to work. could you tell me how to close a case in Stackoverflow? – Ujae Kang Sep 09 '13 at 00:42
  • I appreciate the instructions.. I also tried adding " MaxRows = maxRows; MaxCols = maxCols; for the constructor and it appears to be working as well. Great! – Ujae Kang Sep 09 '13 at 00:45
  • In the comment above you were using the members before setting their value. We don't "close" questions in SO, but the general approach is if you found one answer more useful than others then accept that answer so people know it was useful. – John3136 Sep 09 '13 at 01:47
1

Naming your class member appropriately will help you here. Rename your members and initialize them when the class is constructed.

It's confusing that your arguments are named the same as your members.

TwoD(int maxRows, int maxCols)
    : m_maxRows(maxRows)
    , m_maxCols(maxCol)
Aesthete
  • 18,622
  • 6
  • 36
  • 45