Raghav
Raghav

Reputation: 39

Why is this copy constructor not working?

g++ says there is no match for the call (Order) (Order&), when it comes to a line in the main program which calls order's copy constructor.

Order.h- It contains the class order

class Order
{

private:

int orderNo;



public:

Order()
{}

//Copy Constructor
Order(Order &o)
{
    orderNo = o.putOrderNo();
}

Order(int i)
{
    orderNo = i;
}

};

the erroring code is in the main file-

bool setCurrentOrder(Order o)
{
     CurrentOrder(o);     //currentOrder is a global var defined elsewhere.
                          //after this, there are some comparisons to determine 
                          //whether the object was copied, then true or false is returned.
}     

The compiler refuses to accept the line "CurrentOrder(o)" which copies details of o into another object of class Order called CurrentOrder. Do I have to overload the = operator or is there another way to overcome this?

EDIT

This code is outdated, I have since fixed this problem and I have also reworked this and other code, so I now have some new ones which I shall ask about soon!

Upvotes: 1

Views: 121

Answers (2)

Raghav
Raghav

Reputation: 39

Well, my problem was solved by just overloading the = operator.

Basically, I wanted to use copy constructors to define how the object would be copied, however, operator overloading was probably the best option under the circumstances, because my intention was to assign the values in the old object to the new one. This has been explained in the other answer by Pete.

here is how it was done-

void operator=(Order &o)
{
 orderNo = o.orderNo;
}

Thank you for the constructive criticism. I'll keep it in mind when asking more questions.

Upvotes: 0

Pete Becker
Pete Becker

Reputation: 76245

A copy constructor constructs a new object. Since CurrentOrder already exists, it doesn't make sense to construct it again: CurrentOrder(o) is meaningless. To assign the value of o to CurrentOrder use assignment: CurrentOrder = o;.

Upvotes: 9

Related Questions