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
- How did it work without the copy constructor in the first place.
- 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?
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
QuadTreeby value a copy constructor is needed to perform these copies and hence the compiler complains.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.