これが私のコードです:
//---------------------------------------------------------------------------
#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(constWheel&)」に一致するものが見つかりませんでした
行で:
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);
私は何が間違っているのですか?