Skip to content

2D array through classes in C++

An answer to this question on Stack Overflow.

Question

I am new to C++, well programming in general. I am trying to learn syntax in C++ and I am currently trying to print a 6X6 matrix through a class. I have attached the code below. I am supposed to get a 6X6 matrix filled with zeros but instead I am getting some other value. I do not have this problem if I print it directly from main(). Please see the code and out put attached below (matrix c and B)

Thanks,

#include <iostream>
class test {
public:
	test();
	~test() {};
	int c[6][6];
	int print();
};
test::test() {
	int c[6][6] = { 0 };
}
int test::print() {
	for (int r = 0; r < 6; r++) {
		for (int q = 0; q < 6; q++) {
			cout << c[r][q] << " ";
		}cout << endl;
	}
	return 0;
}
int main()
{
	int B[4][4] = { 0 };
	test a;
	a.print();
	std::cout << endl;
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			std::cout << B[i][j] << " ";
		}
		std::cout << std::endl;
	}
	std::cout << std::endl;
    return 0;
}

Program's output:

-858993460 -858993460 -858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

Answer

The Problem

The immediate problem is that your constructor includes the line

int c[6][6] = {0};

but, this shadows c, and therefore, the values you assign do not propagate to the class scope.

Detecting the Problem

You could have figured this out easily for yourself... if you'd enabled warnings on your compiler. (You may not have known: use warnings to your advantage in the future!)

By compiling your code with:

g++ temp2.cpp -Wall

I get the following handy message:

temp2.cpp: In constructor test::test():
temp2.cpp:15:9: warning: unused variable c [-Wunused-variable]
     int c[6][6] = { 0 };

Which indicates the exact line that needs attention, though perhaps not the symptoms of the problem (the compiler worries that you don't use c locally in this function, but perhaps should have warned about shadowed variables - oh well).

Improving the Code

Since you are using C++, you can take advantage of the std::vector class to handle memory management of your array. You can also declare your print class const to prevent any possibility of it changing the values of c. Incorporating these changes, along with an accessor method and flat-array indexing gives the following:

#include <iostream>
#include <vector>
class Test {
 public:
  std::vector<int> c;
  int width;
  int height;
  Test() = default;
  Test(int width0, int height0, int init_val=0){
    width  = width0;
    height = height0;
    c.resize(width*height,init_val);
  }
  int operator()(int x, int y) const {
    return c[y*width+x];
  }
  int& operator()(int x, int y) {
    return c[y*width+x];
  }
  void print() const {
    for(int y=0;y<height;y++){
      for(int x=0;x<width;x++)
        std::cout<<operator()(x,y)<<" ";
      std::cout<<std::endl;
    }
  }
};
int main(){
  Test t(6,6,0);
  t.print();
  return 0;
}