C++ Classes: Loops in Constructor

  • Thread starter Thread starter TeTeC
  • Start date Start date
  • Tags Tags
    Loops
Click For Summary
SUMMARY

The discussion focuses on the implementation of a default constructor for a matrix class in C++. The user encountered a runtime error related to memory access when using nested loops to initialize a 3x3 matrix. The code snippet provided shows the incorrect loop condition in the inner loop, which should be 'j < COL' instead of 'i < COL'. The user successfully initialized the matrix using a brute force method, confirming that the issue lies within the loop structure.

PREREQUISITES
  • Understanding of C++ class structures
  • Knowledge of operator overloading in C++
  • Familiarity with nested loops and array indexing
  • Experience with debugging runtime errors in C++
NEXT STEPS
  • Review C++ array initialization techniques
  • Learn about memory management in C++
  • Explore debugging tools in Visual Studio Express
  • Study best practices for implementing constructors in C++ classes
USEFUL FOR

C++ developers, software engineers, and students learning object-oriented programming who are interested in class design and debugging techniques.

TeTeC
Messages
55
Reaction score
0
Hello!

I'm experimenting with classes in C++. It works fine for now, but there is something I can't find an explanation for. I tried to develop a 'matrix' class, and I overloaded some operators in order to define addition or multiplication for matrices. This works like a charm, except for the part where I make a default constructor to initialize my matrix.

In order to do this, I first thought about doing a double loop to initialize all the values in the matrix. In my class CMatrix, the fragment of code is:

Code:
	CMatrix () {	
		for (int i = 0; i < ROW; i++){
			for (int j = 0; i < COL; j++){
				matrix[i][j] = 0;
			}
		}
        };

Note: that's how I defined ROW and COL:

#define ROW 3
#define COL 3

However, it doesn't work. The compilation is fine, but at some point when the program is executed, Visual Express opens a windows where it is written:

'Violation of access at 0x00030ffc, etc.' (I can't translate everything, I have the french version of the software, but there is nothing else important to say.)

Since my matrices are 3x3, I tried to initialize all the values by 'brute force':

Code:
CMatrix () {	
	matrix[0][0] = 0;
	matrix[1][0] = 0;
	matrix[2][0] = 0;
	matrix[0][1] = 0;
	matrix[1][1] = 0;
	matrix[2][1] = 0;
	matrix[0][2] = 0;
	matrix[1][2] = 0;
	matrix[2][2] = 0;
};

And it works! I don't see any mistakes in my loops (and VE would have found them). So, is there something I am not allowed to do?

Thank you!
 
Last edited:
Computer science news on Phys.org
Woops, that's the wrong sub-forum. Could somebody move this thread and delete this message ? Thanks.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 4 ·
Replies
4
Views
1K