0

Here is my code:

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

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

I using this code to create a RacingCar object:

RacingCar test();

Yet am getting the following error:

[BCC32 Error] Question 4.cpp(48): E2285 Could not find a match for 'Wheel::Wheel(const Wheel&)'

At line:

Wheel carWheels = new Wheel[3];

I am wanting to create an array of 4 wheels as an array of objects on the heap.

What am I doing wrong?

UPDATE

I am wanting to use a copy constructor for class RacingCar that will create a deep copies of RacingCar objects and then write code to prove that the copy of the RacingCar object is a deep copy.

Can I please have some help to do this?

Here is my code:

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

* 2nd UPDATE

Here is my current code:

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

The copy constructor is not working correctly. I am getting an error at:

Wheel oldObjectWheel = oldObject.getWheel(i);

What am I doing wrong?

trincot
  • 317,000
  • 35
  • 244
  • 286
Darryl Janecek
  • 399
  • 4
  • 9
  • 25
  • 2
    Also you probably want `Wheel[4]` - most cars don't have only 3 :) – Blorgbeard Aug 27 '12 at 04:40
  • if you want to prove deep copy in `RacingCar` I think you should change `Wheel`'s `ptrSize` from a pointer to a value. And write the `ctor` and `operator=` in `RacingCar` – shengy Aug 27 '12 at 05:52

3 Answers3

3

That's a syntax error, it should be

 Wheel* carWheels = new Wheel[3];

new returns a pointer type - a pointer to the very first Wheel in the array, so carWheels[i] works as intended to access the ith wheel.

Consider allocating four (4) wheels unless you're sure your car is ok with three (3).

Alexander Gessler
  • 45,603
  • 7
  • 82
  • 122
  • Doesn't Wheel* carWheels = new Wheel[3] have 4 items in the array? 0,1,2,3? – Darryl Janecek Aug 27 '12 at 04:54
  • No, big warning on that. The indexing is zero-based, but not the numbers themselves. `new Wheel[4]` creates four `Wheel` instances, accessible by indices 0,1,2,3. `new Wheel[0]` creates an empty array. – Alexander Gessler Aug 27 '12 at 04:56
  • Hint: follow the rule of three for `Wheel`, then do a plain element-wise copy (i.e. `new_arr[0] = old_arr[0]; ..`). Proof their independence by verifying that their `ptrSize` members have different addresses. – Alexander Gessler Aug 27 '12 at 05:24
  • What is a 'plain element-wise copy'? – Darryl Janecek Aug 27 '12 at 06:11
1

You're trying to create a Wheel called carWheels and initialize its value to a pointer to an array of 3 Wheels. You probably want:

Wheel* carWheels = new Wheel[3];
David Schwartz
  • 179,497
  • 17
  • 214
  • 278
0

I wrote an example of deep-copy, wish this helps.

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

#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;
}
shengy
  • 9,461
  • 4
  • 37
  • 61
  • I have made a 2nd Update to my code. Can you please have a look at it? – Darryl Janecek Aug 27 '12 at 09:05
  • @DarrylJanecek You got the error because in your `RacingCar`'s copy constructor, `oldObject.getWheel(i);` is not a const function, and is called by a `const RacingCar&`. Change `Wheel getWheel(int id)` to `Wheel getWheel(int id) const` will fix this compile error, but I think you are not understanding deep copy here. I think you don't need to maintain a pointer in the `Wheel` class, all you need to deep copy is the `Wheel*`'s in your `RacingCar` class, please read http://www.learncpp.com/cpp-tutorial/912-shallow-vs-deep-copying/ – shengy Aug 28 '12 at 03:42