2014-09-26 3 views
0

컴파일 오류가 발생하지 않고 다른 클래스의 하나의 클래스 포인터 배열로이 작업을 시도 할 때 오류가 발생하지 않습니다. 내가 세 가지 수업을 할 때. 나는 추락한다.다른 클래스의 포인터 포인터 배열의 클래스 포인터 배열의 세그먼트 오류

#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; 
} 

저는 왜 pointRow에서 작동하지만 현재는 totalSquare인지 알 수 없습니다.

답변

1

귀하의 totalSquare 케이스 어디서나 makePointRowArray을 호출하지 않습니다. 그것 없이는 pointRowArray의 포인터는 초기화되지 않으며 프로그램을 중단시킬 수 있습니다. 당신은 아마 다음과 같은 기능에 추가하여이 문제를 해결할 수 있습니다

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

당신은 당신이 당신의 객체를 사용하면 데이터를 초기화하는 것을 잊지 마세요 있도록 생성자에이 일을 고려해야합니다.

+0

생성자를 만들고 사용합니다. 정말 고마워. – 1N5818