1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Loops in constructor

  1. Jun 21, 2008 #1
    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 (Text):
        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 (Text):
    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: Jun 21, 2008
  2. jcsd
  3. Jun 21, 2008 #2
    Woops, that's the wrong sub-forum. Could somebody move this thread and delete this message ? Thanks.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Loops in constructor
  1. Loops in C-Programming (Replies: 15)

  2. C++ for loop help (Replies: 6)

  3. While loop in C (Replies: 11)

Loading...