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

当我在 main 中调用它时会出现问题

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 中以创建一个矩形我创建另一个分数

然后我将 Rectangle 和分数传递给 Box

我知道问题与我制作的默认构造函数有关。请帮助:) 我知道我可以使用普通会员数据

4

2 回答 2

0

一个问题是构造函数在指针被初始化之前取消引用指针,从而导致未定义的行为RectangleFraction

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

您需要为和创建一个Fraction对象。lenwid

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这将自动初始化和销毁​​对象,而无需调用newdelete

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

Rectangle::Rectangle(Fraction& frac, Fraction& frac1)
{
    // ....
}
于 2013-05-23T12:04:40.853 回答
0

由于您没有提及具体的错误是什么,因此只能猜测...但是,由于您class Box ....不包含对带有两个引用的构造函数的声明(即使您似乎已经为框外的一个提供了定义),尝试调用new Box(x, y)将失败,因为它找不到正确的构造函数来调用。您也应该在Box接受参数的构造函数的定义上遇到错误,因为它们没有在class Box正文中声明。还有很多其他问题,但这些都是与您标记的线路特别相关的问题。

于 2013-05-23T15:12:00.003 回答