0

これが私のコードです:

//---------------------------------------------------------------------------

#pragma hdrstop

#include <tchar.h>
#include <string>
#include <iostream>
#include <sstream>
#include <conio.h>

using namespace std;

//---------------------------------------------------------------------------

class Wheel
{
public:
        Wheel()
    {
        pressure = 32;
        ptrSize = new int(30);
    }
    Wheel(int s, int p)
    {
        ptrSize = new int(s);
        pressure = p;
    }
    ~Wheel()
    {
        delete ptrSize;
    }
    void pump(int amount)
    {
       pressure += amount;
    }

private:
    int *ptrSize;
    int pressure;
};

class RacingCar
{
public:
    RacingCar()
    {
        speed = 0;
        Wheel carWheels = new Wheel[3];
    }
    RacingCar(int s)
    {
        speed = s;
    }
    void Accelerate()
    {
        speed = speed + 10;
    }

private:
    int speed;
};

このコードを使用してRacingCarオブジェクトを作成します。

RacingCar test();

しかし、次のエラーが発生しています。

[BCC32エラー]質問4.cpp(48):E2285「Wheel :: Wheel(c​​onstWheel&)」に一致するものが見つかりませんでした

行で:

Wheel carWheels = new Wheel[3];

ヒープ上のオブジェクトの配列として4つのホイールの配列を作成したいと思います。

私は何が間違っているのですか?

アップデート

RacingCarオブジェクトのディープコピーを作成し、RacingCarオブジェクトのコピーがディープコピーであることを証明するコードを記述するRacingCarクラスのコピーコンストラクターを使用したいと考えています。

これを行うために助けてもらえますか?

これが私のコードです:

class RacingCar
{
public:
    RacingCar()
    {
        speed = 0;
        Wheel* carWheels = new Wheel[3];
    }
    RacingCar(int s)
    {
        speed = s;
    }
    RacingCar(const RacingCar &oldObject)
    {
        //I am not sure what to place here.
        Wheel* carWheels = new Wheel[3];

    }
    void Accelerate()
    {
        speed = speed + 10;
    }

private:
    int speed;
};

*2回目の更新

これが私の現在のコードです:

class Wheel
{
public:
    Wheel() : pressure(32)
    {
        ptrSize = new int(30);
    }
    Wheel(int s, int p) : pressure(p)
    {
        ptrSize = new int(s);
    }
    ~Wheel()
    {
        delete ptrSize;
    }
    void pump(int amount)
    {
        pressure += amount;
    }
    int getSize()
    {
        return *ptrSize;
    }
    int getPressure()
    {
        return pressure;
    }
private:
    int *ptrSize;
    int pressure;
};

class RacingCar
{
public:
    RacingCar()
    {
        speed = 0;
        *carWheels = new Wheel[4];
    }
    RacingCar(int s)
    {
        speed = s;
    }
    RacingCar(const RacingCar &oldObject)
    {
        for ( int i = 0; i < sizeof(carWheels)/sizeof(carWheels[0]); ++i)
        {
            Wheel oldObjectWheel = oldObject.getWheel(i);
            carWheels[i]=new Wheel(oldObjectWheel.getSize(),oldObjectWheel.getPressure());
        }

    }
    void Accelerate()
    {
        speed = speed + 10;
    }
    Wheel getWheel(int id)
    {
        return *carWheels[id];
    }
private:
    int speed;
    Wheel *carWheels[4];
};

コピーコンストラクターが正しく機能していません。次の場所でエラーが発生します:

Wheel oldObjectWheel = oldObject.getWheel(i);

私は何が間違っているのですか?

4

3 に答える 3

3

これは構文エラーです。

 Wheel* carWheels = new Wheel[3];

newWheelポインタ型を返します-配列の最初のポインタへのポインタなので、 thホイールcarWheels[i]にアクセスするために意図したとおりに機能します。i

車が3つで問題ないことが確実でない限り、4つのホイールを割り当てることを検討してください。

于 2012-08-27T04:39:14.457 に答える
1

Wheel呼び出されたものを作成し、carWheelsその値を3秒の配列へのポインターに初期化しようとしていますWheel。あなたはおそらく欲しい:

Wheel* carWheels = new Wheel[3];
于 2012-08-27T04:39:28.647 に答える
0

ディープコピーの例を書きました。これがお役に立てば幸いです。

//---------------------------------------------------------------------------

#include <iostream>

using namespace std;

//---------------------------------------------------------------------------

class Demo
{
public:
    // Constructor
    Demo()
    {
        ptrNeedsDeepCopy = new int(10);
    }

    // Copy constructor
    Demo(const Demo& rhs)
    {
        // You need to allocate new memory to make sure the two
        // objects are not pointing to the same memeory.
        ptrNeedsDeepCopy = new int(*(rhs.ptrNeedsDeepCopy));
    }

    // Copy assign operator
    Demo& operator=(const Demo& rhs)
    {
        // Do things same in the copy constructor
        // AND!!! you have to check that the object is not
        // assigning it to itself.
        if(this != &rhs)
        {
            ptrNeedsDeepCopy = new int(*(rhs.ptrNeedsDeepCopy));  
        }
        return *this;
    }

    // Destructor
    ~Demo()
    {
        delete ptrNeedsDeepCopy;
    }

    void SomeMemberFunctions()
    {
        // What ever.
    }

    int GetPtrValue()
    {
        if(ptrNeedsDeepCopy)
        {
            return *ptrNeedsDeepCopy;
        }
    }

    void SetPtrValue(int val)
    {
        if(ptrNeedsDeepCopy)
        {
            *ptrNeedsDeepCopy = val;  
        }
    }

private:
    int *ptrNeedsDeepCopy;

};

int main()
{
    Demo a;
    Demo b = a;

    cout << "a's initial value: " << a.GetPtrValue() << endl;
    cout << "b's initial value: " << b.GetPtrValue() << endl;


    a.SetPtrValue(7);

    cout << endl;

    cout << "a's changed value: " << a.GetPtrValue() << endl;
    cout << "b's changed value: " << b.GetPtrValue() << endl;

    return 0;
}
于 2012-08-27T06:57:15.547 に答える