-2

I am writing a simple header file that can make a dynamic 2d array and put 0 in the row and col, print the array, and delete the array.

In Debug when stepping through, the 2d array gets initialized, it puts 0 in there, however when my_array.Print_Array(); called, the compiler skips it.

when i try to print the array from the main file it fails. any help would be appreciate it.

HEADER FILE:

        class twoD_Array
{
    public:
        int **Array;
        int *col, *row;
        int size_col, size_row;

        twoD_Array(int, int);
        ~twoD_Array();

        void Print_Array();

};

twoD_Array::twoD_Array(int size_c, int size_r)
{       
        size_col = size_c;
        size_row = size_r;

        Array = new int *[size_col];
        for (int i = 0; i < size_col; i++)
        {
            Array[i] = new int[size_row];

                for (int j = 0; j < size_row; j++)
                    Array[i][j] = 0;
        }

}

void twoD_Array::Print_Array()
{
    for (int y_i = 0; y_i<size_col; y_i++)
    {
        for (int x_i = 0; x_i<size_col; x_i++)
            std::cout << Array[y_i][x_i];

        std::cout << std::endl;
    }
}

twoD_Array::~twoD_Array()
{
    for (int i = 0; i < size_row; i++)
        delete[] Array[i];

    delete[] Array;
}

Main File:

#include "stdafx.h"
#include <iostream>
#include "2D_Array.h"

int main()
{
    int x, y;

    std::cout << "how many x variables?" << std::endl;
    std::cin >> x;
    std::cout << "how many y variables?" << std::endl;
    std::cin >> y;

    twoD_Array my_array(x, y);
    my_array.Print_Array();


    return 0;
}
pinkfloydx33
  • 11,863
  • 3
  • 46
  • 63
BlooB
  • 955
  • 10
  • 23
  • Any reason for not using `std::vector` to manage the underlying array? – WhiZTiM Feb 18 '17 at 20:10
  • You run one of your nested for cycle until size_col instead size_row – pergy Feb 18 '17 at 20:11
  • `when i try to print the array from the main file it fails` can you define _it fails_ ? – Yousaf Feb 18 '17 at 20:13
  • WHizTim I am just trying to learn how to use arrays, next i will learn how to use vectors. – BlooB Feb 18 '17 at 20:17
  • Yousaf when i use my_array.Print_Array(); nothing shows up on the command window, it simply waits for me to exit – BlooB Feb 18 '17 at 20:18
  • 1
    Where do you initialise member variables `size_col` and `size_row` with constructor parameters `size_col` and `size_row`? It doesn't happen automatically, so if you don't, the members will be uninitialised. – Justin Time - Reinstate Monica Feb 18 '17 at 20:28
  • @JustinTime you are correct that fixed the problem, please put your comment in answer section, so i can pick as answer. – BlooB Feb 18 '17 at 20:32
  • Please don't edit the solution into the question itself; it can make it harder for people with similar problems to find solutions. Feel free to put it in an answer, or accept either of the two answers that mentions it, though. – Justin Time - Reinstate Monica Feb 18 '17 at 20:58

2 Answers2

3

You are using the local variables in the constructor, but you're using the member variables in the Print_Array method, which are not initialized. You need to initialize the member variables size_col and size_row in the constructor where they are provided. Also, another thing to point out, in Print_Array method, you are using size_col instead of size_row for the x_i loop, which looks like a logical error. I've rewritten the constructor and Print_Array to look this way:

twoD_Array::twoD_Array(int size_col, int size_row)
{
    this->size_col = size_col;
    this->size_row = size_row;
    Array = new int *[size_col];
    for (int i = 0; i < size_col; i++)
    {
        Array[i] = new int[size_row];
        for (int j = 0; j < size_row; j++)
        {
            Array[i][j] = 0;
        }
    }
}

void twoD_Array::Print_Array()
{
    for (int y_i = 0; y_i < size_col; y_i++)
    {
        for (int x_i = 0; x_i < size_row; x_i++)
        {
            std::cout << Array[y_i][x_i];
        }

        std::cout << std::endl;
    }
}
3

In

twoD_Array::twoD_Array(int size_col, int size_row)

int size_col defines a new temporary and local to the constructor variable named size_col. This size_col has nothing to do with the member variable size_col and because they have the same name the local variable hides the member variable.

End result is in Print_Array the member variable size_col has not been set to anything, so Crom only knows what will happen.

Solution:

Set the member variable:

twoD_Array::twoD_Array(int col, int row): size_col(col), size_row(row)

The colon (:) tells the compiler that a Member Initializer List is coming. The Member Initializer List allows you to initialize class member before entering the body of the constructor. This is very important when you have a member variable that has no default constructor or requires expensive initialization you don't want to be forced to do twice. It' also allows you to initialize a base class.

Note that I also changed the names of the parameters to prevent future confusion.

And while we are here, let's head off what is likely to be OP's next problem by asking What is The Rule of Three? Seriously. Click the link. Save you a ton of debugging.

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54