2

I have a class called QuadTree. I have recently created a copy constructor for it ie.:

QuadTree(const QuadTree &cpy);

Say for example I have not yet filled out this constructor. As long as it is not used the code will compile just fine. Now, I have a function called subtractTrees:

QuadTree * subtractTrees(QuadTree LHS, QuadTree RHS);

Previous to making the copy constructor this code worked just fine. Now when compiling a program using this function i get the following error:

Undefined Referance to QuadTree::QuadTree(QuadTree const&)

As in the error that would occur because my copy constructor is used in the code and is not yet filled out. Does this mean that now that I have a copy constructor calls to passive functions like this (subtractTrees) will call the copy constructor?

If so is there a way to stop this from occurring whilst still using the copy constructor? The reason I need to do this is that copying for use in functions like this will vastly slow down my code. But I need the copy constructor to easily copy trees.

EDIT: I have fixed the error simply by filling out the copy constructor, but the question is more about

  1. How did it work without the copy constructor in the first place.
  2. Is there a way of utilising this no-need for copy constructor if one was trying to save on speed by not copying the tree every time it is used?

2 Answers 2

4

You must have an constructor defined for your class QuadTree, when you do that the compiler does not generate the implicit copy constructor, it assumes you will provide your own if you need it.

When you add the function, which takes the type QuadTree by value a copy constructor is needed to perform these copies and hence the compiler complains.

QuadTree * subtractTrees(QuadTree LHS, QuadTree RHS);
                         ^^^^^^^^^^^^  ^^^^^^^^^^^^^

Pass by value needs copy constructor.

If so is there a way to stop this from occurring whilst still using the copy constructor?

I am not sure I understand the question. You need a copy constructor if you want to create an copy of your class object. If you want to avoid the copies pass your object by const reference.

QuadTree * subtractTrees(const QuadTree &LHS, const QuadTree &RHS);
5
  • So is that it, If I will have to fill it out and live with potentially slow consequences, how was the code working before without the copy constructor? May 10, 2012 at 4:21
  • @Ben: By assuming fill means providing a definition, Your code worked before because prior to providing the new function your code did not need a copy constructor to be invoked anywhere.
    – Alok Save
    May 10, 2012 at 4:23
  • @Als I may have mis-worded the question, the subtract function worked well without a copy constructor ever have being produced. How is this possible if you need a copy to pass by value? May 10, 2012 at 4:26
  • I have filled out the copy constructor and it works fine, I am still interested to know if there is a way around using it though if i become desperate for speed. May 10, 2012 at 4:28
  • @Ben: The first para of the answer, explains that. Check the updated answer for your second query.
    – Alok Save
    May 10, 2012 at 4:29
1

When you hadn't declared the copy constructor, the compiler implicitly provided one for you and that is why your code worked before. Since you are passing QuadTree by value to subtractTrees, this implicit copy constructor was being called before you declared your own copy constructor. Once you declared (but didn't define) your own copy constructor the compiler stopped implicitly defining a copy constructor one so all the previous calls to the copy constructor (such as in your subtractTrees function) produce undefined references.

If you want to avoid copies in your subtractTrees function you can pass the paramaters by reference:

QuadTree * subtractTrees(const QuadTree &LHS, const QuadTree &RHS);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.