C++ Classes: Loops in Constructor

  • Thread starter TeTeC
  • Start date
  • Tags
    Loops
In summary, the conversation is about a programmer experimenting with classes in C++ and developing a 'matrix' class. They have overloaded some operators for addition and multiplication, but are having trouble with their default constructor to initialize the matrix. They have tried using a double loop and a 'brute force' method, and the latter works. They are wondering if there is something they are not allowed to do.
  • #1
TeTeC
55
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
  • #2
Woops, that's the wrong sub-forum. Could somebody move this thread and delete this message ? Thanks.
 
  • #3



Hello! It seems like you are experiencing an issue with your default constructor in your 'matrix' class. It is possible that the issue lies with the way you are initializing your matrix in the constructor. Without being able to see the rest of your code, it is difficult to pinpoint the exact issue. However, there are a few things you can try to troubleshoot this problem.

First, make sure that your matrix is properly declared and initialized in your class. It is possible that you may have missed a step in setting up your matrix variable.

Second, check your loop conditions to make sure they are correct. In your first loop, you have 'i < ROW' but in your second loop, you have 'i < COL'. This may be causing an issue with accessing values outside of the matrix bounds.

Third, double check that your matrix is not being accessed out of bounds in any other part of your code. This could also be causing the issue.

If none of these solutions work, it may be helpful to provide more of your code or to try debugging the program to see where the issue is occurring. Good luck with your experimentation!
 

What is a C++ class?

A C++ class is a user-defined data type that contains data members (variables) and member functions (methods) to operate on those data members. It serves as a blueprint for creating objects, which are instances of the class.

What is a constructor in C++?

A constructor is a special member function of a class that is used to initialize the data members of an object when it is created. It has the same name as the class and is automatically called when an object is created.

How do you create a loop in a C++ constructor?

A loop can be created inside a C++ constructor by using any of the three types of loops: for, while, or do-while. The loop can be used to perform iterations on a set of instructions or statements that are to be executed when the constructor is called.

Why would you use a loop in a C++ constructor?

A loop in a C++ constructor can be used to initialize multiple data members of an object or to perform a repetitive task during the object's creation. This can save time and reduce the amount of code needed in the constructor.

Can a C++ constructor have multiple loops?

Yes, a C++ constructor can have multiple loops. This allows for more complex initialization of data members or for performing different tasks during the object's creation. It is important to ensure that the loops do not create any conflicts or affect the overall functionality of the constructor.

Similar threads

  • Programming and Computer Science
Replies
1
Views
909
  • Precalculus Mathematics Homework Help
Replies
1
Views
725
  • Programming and Computer Science
Replies
25
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Linear and Abstract Algebra
Replies
1
Views
806
  • Calculus and Beyond Homework Help
Replies
2
Views
386
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Replies
1
Views
545
Replies
6
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
3K
Back
Top