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(const Wheel&)'

在线:

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

* 第二次更新

这是我当前的代码:

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

new返回一个指针类型 - 指向Wheel数组中第一个的指针,因此carWheels[i]按预期工作以访问第ith 个轮子。

考虑分配四 (4) 个车轮,除非您确定您的汽车可以安装三 (3) 个车轮。

于 2012-08-27T04:39:14.457 回答
1

您正在尝试创建一个Wheel被调用并将其值初始化为指向 3 scarWheels数组的指针。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 回答