C++ Classes: Loops in Constructor

  • Thread starter Thread starter TeTeC
  • Start date Start date
  • Tags Tags
    Loops
AI Thread Summary
The discussion revolves around a user experimenting with a C++ class for matrices, specifically facing issues with the default constructor for initializing a 3x3 matrix. The user initially attempted to use nested loops to set all matrix values to zero, but encountered a runtime access violation error. However, a manual initialization approach, where each element is set individually, worked without issues. The user is puzzled by the failure of the loop method, suspecting there may be restrictions or errors in their implementation. The thread concludes with a request to move the discussion to a more appropriate sub-forum.
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.
 
Thread 'ChatGPT Examples, Good and Bad'
I've been experimenting with ChatGPT. Some results are good, some very very bad. I think examples can help expose the properties of this AI. Maybe you can post some of your favorite examples and tell us what they reveal about the properties of this AI. (I had problems with copy/paste of text and formatting, so I'm posting my examples as screen shots. That is a promising start. :smile: But then I provided values V=1, R1=1, R2=2, R3=3 and asked for the value of I. At first, it said...
Sorry if 'Profile Badge' is not the correct term. I have an MS 365 subscription and I've noticed on my Word documents the small circle with my initials in it is sometimes different in colour document to document (it's the circle at the top right of the doc, that, when you hover over it it tells you you're signed in; if you click on it you get a bit more info). Last night I had four docs with a red circle, one with blue. When I closed the blue and opened it again it was red. Today I have 3...
Back
Top