0

The error is: No instance of overloaded function "BSTree::Retrieve" matches argument list and object (the object has type qualifiers that prevent a match).

argument types are: (int, Account*, BSTree::Node*const)

object type is: const BSTree

It's saying the argument type is Account * but I have it as Account *&acct.

First parameter is object to retrieve. Second holds pointer to found object.

Here's the code:

bool BSTree::Retrieve(int ID, Account *&acct, Node *leaf)
{
    if(leaf != NULL)
    {
        if(ID == leaf->pAcct->getID())
        {
            acct = leaf->pAcct;
            return true;
        }
        if(ID < leaf->pAcct->getID())
        {
            return Retrieve(ID, acct, leaf->left);
        }
        else
        {
            return Retrieve(ID, acct, leaf->right);
        }
    }
    else
    {
        acct = NULL;
        return false;
    }
}

bool BSTree::Retrieve(int ID, Account *&acct) const
{
    return Retrieve(ID, acct, root);
}
Kara
  • 6,115
  • 16
  • 50
  • 57
Bapho
  • 63
  • 1
  • 4
  • possible duplicate of [Passing references to pointers in C++](http://stackoverflow.com/questions/823426/passing-references-to-pointers-in-c) – Nick Louloudakis Feb 11 '15 at 15:13

2 Answers2

0
  1. You are calling a non-const method (which can modify the object) from a const method (which is not allowed to do so). It will be an error.

  2. Are you sure you want Account *& ? From your code, if you want to retrieve a node by it's ID from the BSTree, you will need a Account*.

For point 1 see here.

a.g(); // invokes an error because g() (which is const) calls f() which is non-const.
a_pradhan
  • 3,285
  • 1
  • 18
  • 23
0

Account *&acct is a pointer to a reference. That means that acct will act as a reference to a pointer given as argument. What your function expects as argument is passing an actual pointer and not an existing reference.

You can try this:

bool BSTree::Retrieve(int ID, Account *&acct) const
{
    Account* acctPtr = acct;
    return Retrieve(ID, acctPtr, root);
}

You can look at a similar case here.

Apart from that, you are trying to invoke a non-const method from a const one. You could use const_cast in order to fix this but it is NOT recommended as it is dangerous and you should search for better alternatives (look here for more).

Community
  • 1
  • 1
Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54