0

I'm not getting any compilation errors nor am I getting any errors when try to do this with only one class pointer array of another class. When I do it with three classes. I crash.

#define size 10

// single point with x and y
class singlePoint{
public: 
    float xCoordinate;
    float yCoordinate;  
};

// row of points
class pointRow{
public: 
    singlePoint *pointRowArray[size];
    void makePointRowArray(int);    
    void setPointRowArray(int); 
};

// “row” of row of points
class totalSquare{
public: 
    pointRow *totalSquareArray[size];
    void makeTotalSquare(int);
    void setTotalSquare(int); 
};
//-----------------
void pointRow::makePointRowArray(int rowSize){
    for (int i = 0; i < rowSize; i++){
        pointRowArray[i] = new singlePoint;
    }
}

void pointRow::setPointRowArray(int rowSize){
    for (int i = 0; i < rowSize; i++){
        pointRowArray[i]->xCoordinate = 11;
        pointRowArray[i]->yCoordinate = 12;
    }
}

void totalSquare::makeTotalSquare(int collumnSize){
    for (int i = 0; i < collumnSize; i++){
        totalSquareArray[i] = new pointRow;
    }   
}

void totalSquare::setTotalSquare(int collumnSize){
    for (int collumnSet = 0; collumnSet < collumnSize; collumnSet++){   
        for (int rowSet = 0; rowSet < collumnSize; rowSet++){
            // my problem lies somewhere in here
            totalSquareArray[collumnSet]->pointRowArray[rowSet]->xCoordinate = 13;
            totalSquareArray[collumnSet]->pointRowArray[rowSet]->yCoordinate = 14;
        }
    }   
}


int main(void){
    // this was just a test for pointRow and it’s fine
    pointRow firstRow;
    firstRow.makePointRowArray(size);
    firstRow.setPointRowArray(size);    

    // this works up until…
    totalSquare firstSquare;
    firstSquare.makeTotalSquare(size);
    // this point. I cannot assign 25
    // I either get a bus error 10 or segmentation fault. what am I doing wrong?
    firstSquare.totalSquareArray[0]->pointRowArray[0]->xCoordinate = 25;


    return 0;
}

I just don't know why it works for pointRow but now totalSquare.

1 Answer 1

1

You are not calling makePointRowArray anywhere in your totalSquare case. Without that, the pointers in pointRowArray are uninitialised and are likely to crash your program. You can probably fix this by adding it to the following function:

void totalSquare::makeTotalSquare(int collumnSize){
    for (int i = 0; i < collumnSize; i++){
        totalSquareArray[i] = new pointRow;
        totalSquareArray[i]->makePointRowArray(size);
    }   
}

You should consider doing this in a constructor so that you don't forget to initialise your data when using your objects.

Sign up to request clarification or add additional context in comments.

1 Comment

Will make and use a constructor. Thanks so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.