2013-05-23 3 views
0

다음은 3 개의 클래스입니다. 분수/직사각형/박스두 개체를 전달하면서 처리되지 않은 예외가 발생 했습니까?

class Fraction { 
public: 
    Fraction(); 
    Fraction(int); 
    Fraction(int, int); 
    ~Fraction(); 
    void setNum(int); 
    void setDenom(int); 
    int getNum(void) const; 
    int getDenom(void) const; 
    void print(void); 
private: 
    int num; 
    int denom; 
}; 

class Rectangle { 
public: 
    FinalRectangleRavuthL(); 
    ~FinalRectangleRavuthL(); 
    FinalRectangleRavuthL(Fraction&, Fraction&); 
    void setLen(Fraction arg); 
    void setWid(Fraction arg); 
    Fraction getLen(void) const; 
    Fraction getWid(void) const; 
    friend ostream& operator<<(ostream&, Rectangle&); 
private: 
Fraction *len; 
Fraction *wid; 
}; 

class Box : public Rectangle { 
public: 
    Box(); 
    FinalBoxRavuthL(Rectangle& arg, Fraction& arg1); 
    FinalBoxRavuthL(Fraction& arg); 
    void print(void); 
    void getBox(void); 
    ~FinalBoxRavuthL(); 
private: 
Fraction *height; 
}; 

// 프로그램

#include "Fraction.h" 
#include "Circle.h" 
#include "Box.h" 
#include <iostream> 
using namespace std; 

Fraction::Fraction() { 
    num = 0; 
    denom = 1; 
} 
Fraction::Fraction(int n, int d) { 
    num = n; 
    denom = d; 
} 
Fraction& Fraction::operator=(const Fraction& arg) { 
    num = arg.num; 
    denom = arg.denom; 
    return *this; 
} 
Fraction::~Fraction() { 
} 

//Rectangle 
Rectangle::Rectangle() { 
    *len = FinalFractionRavuthL(2); 
    *wid = FinalFractionRavuthL(1); 
} 
Rectangle::RectangleFraction& frac, Fraction& frac1) { 
    cout << "First Fraction : "; 
    frac.print(); 
    cout << "Second Fraction : "; 
    frac1.print(); 
} 

void Rectangle::setLen(Fraction arg) { 
    len->setNum(arg.getNum()); 
    len->setDenom(arg.getDenom()); 
} 
void Rectangle::setWid(Fraction arg) { 
    wid->setNum(arg.getNum()); 
    wid->setDenom(arg.getDenom()); 
} 
Fraction Rectangle::getLen(void) const { 
    return Fraction(len->getNum(), len->getDenom()); 
} 
Fraction Rectangle::getWid(void) const { 
    return Fraction(wid->getNum(), wid->getDenom()); 
} 
Rectangle::~Rectangle() { 
    delete this->len; 
    delete this->wid; 
} 

//Box 
Box::Box() : height(new Fraction()) 
{ 
    height->setNum(0); 
    height->setDenom(0); 
} 
Box::Box(Rectangle& arg, Fraction& arg1) : height(new Fraction()) 
{ 
    arg.getLen().print(); 
    arg.getWid().print(); 

    cout << arg1.getNum(); 
    cout << arg1.getDenom(); 
} 
Box::Box(Fractio& arg) { 
     height->setNum(arg.getNum()); 
    height->setDenom(arg.getDenom()); 
} 
Box::~Box() { 
    delete this->height; 
} 

내가 주

Fraction* fPtrA = new Fraction(4, 1); 
Rectangle* rPtrA = new Rectangle(*fPtrA, *fPtrA); 
Fraction* fPtrD = new Fraction(9, 1); 

BoxRavuthL* boxPtrA = new Box(*rPtrA, *fPtrD); //PROBLEM 

이를 호출 할 때 문제가 발생합니다 나는 사각형을 만들기 위해 클래스 사각형에 두 개의 분수 fPtrA를 넣어 다른 분수를 생성합니다.

다음으로 Rectangle과 f raigh into Box

나는이 문제가 내가 만든 기본 생성자와 관련이 있다는 것을 알고있다. 나는 일반 회원 데이터

+0

여기에'new'를 사용해야하는 이유는 무엇입니까? – soon

+0

Rectangle 클래스가 ~ FinalRectangleRavuthL()을 소멸자로 사용하려면 어떻게해야합니까?! – ZoomIn

+0

@PeterWood 세 규칙이 위반되었지만 이는 해당 게시물의 중복이 아닙니다. –

답변

0

문제 중 하나를 사용할 수 있습니다 알고 :) 도와주세요 것은 그들이 정의되지 않은 동작의 결과로 초기화하기 전에 Rectangle의 생성자가 Fraction 포인터를 역 참조하는 것입니다.

// len and wid have not been initialized and point to random points in memory 
Rectangle::Rectangle() 
{ 
    *len = FinalFractionRavuthL(2); 
    *wid = FinalFractionRavuthL(1); 
} 

당신은 lenwid에 대한 Fraction 객체를 생성해야합니다.

Rectangle::Rectangle() 
    : len(new Fraction()), wid(new Fraction()) 
{ 
    *len = FinalFractionRavuthL(2); 
    *wid = FinalFractionRavuthL(1); 
} 

Rectangle::Rectangle(Fraction& frac, Fraction& frac1) 
    : len(new Fraction()), wid(new Fraction()) 
{ 
    // .... 
} 

Fraction에 대한 포인터가 정말 당신이 자동 저장 기간이 멤버 변수를 선언하여 new로 할당을 피할 수 필요가 표시되지 않기 때문에. 당신의 class Box ....가 포함되어 있지 않기 때문에이 자동으로 초기화하고 특정 오류가, 하나는 추측 할 수 무엇인지 언급하지 않기 때문에 new

class Rectangle 
{ 
public: 
    // .... 
private: 
    Fraction len; 
    Fraction wid; 
}; 
Rectangle::Rectangle() 
{ 
    len = FinalFractionRavuthL(2); 
    wid = FinalFractionRavuthL(1); 
} 

Rectangle::Rectangle(Fraction& frac, Fraction& frac1) 
{ 
    // .... 
} 
0

delete를 호출하지 않고 개체를 파괴 ...하지만 것 두 개의 참조를 사용하는 생성자에 대한 선언 (상자 외부에 대한 정의를 제공 한 것처럼 보이더라도) new Box(x, y)을 호출하려고하면 호출 할 올바른 생성자를 찾을 수 없으므로 실패하게됩니다. class Box 본문에 선언되지 않았으므로 인수를 사용하는 Box 생성자의 정의에 오류가 발생해야합니다. 다른 많은 문제들도 있습니다. 그러나 이것들은 특별히 신고 한 라인과 관련이 있습니다.

관련 문제