error: no matching function for call to?

Hi guys, I've finished making a Double-Linked List in C++ which basically
changes a Binary number to a Decimal using Recursivity.
but when I compile it, I get an error that says
error: no matching function for call to 'ListaDoblementeLigada::B2AB10Recursivo(int&, int&, NodoDoble)'

note: candidates are: void ListaDoblementeLigada::B2AB10Recursivo(int&, int&, NodoDoble*)

This is a snippet of the code where I get the error (Line 9) :

1
2
3
4
5
6
7
8
9
10
11
12
13
class Programa
{
    public:
    static void Principal()
    {
    int num=0,k=0;
    ListaDoblementeLigada objLista;
    objLista.CrearLista();
    objLista.B2AB10Recursivo(num,k,objLista.RetornaUltimo());
    objLista.MostrarLista();
    cout<<"en base 2 ="<<num<<"en base 10";
    }
};


Here's the code for the void function B2AB10Recursivo (it stands for Base 2 to Base 10 Recursivity in Spanish)

1
2
3
4
5
6
7
8
9
10
void B2AB10Recursivo(int& num, int& k, NodoDoble* p)   
  {
      if(p!=cab)
      {
          num+=(int)((p->dato-48)*pow(2,k));    //pow(2,k)  <---> 2^k  
          k++;
          *p=p->RetornaLigaIzq();
          B2AB10Recursivo(num,k,p);
      }
  }


Im guessing the problem must be with all the ampersand (&) signs, I get confused because I'm not sure which numbers I have to send by reference and which ones I don't.
I'm new here, so I'm not sure if I can paste Spanish codes here so sorry in advance if I broke any rules, any help would be much appreciated.
I'm guessing (as you haven't included the definition) that objLista is a list of NodoDoble objects, and not NodoDoble* pointers?

Your error message is saying that you're trying to call a B2AB10Recursivo function that takes two int references and a NodoDoble - by value. It's also saying that the only possible candidate it can find takes two int refs and a NodoDoble pointer.

This is backed up by your posted code.

So, either line 9 needs to be something like
 
objLista.B2AB10Recursivo(num,k,&objLista.RetornaUltimo());


But that depends on what RetornoUltimo() is returning - a reference or an instance of a NodoDoble?

Jim
Ok i put line 9 as you said, but now it gives a new error:

warning: taking address of temporary


PS. RetornaUltimo() is a simple return command that shows the last link of the list.

1
2
3
4
NodoDoble RetornaUltimo()
  {
      return *ultimo;
  }
OK, I'd suggest returning a reference from RetornaUltimo.

1
2
3
4
NodoDoble& RetornaUltimo()
{
  return ultimo;
}


You'll then be safe to take the address of it in line 9; at the moment you are, as the compiler says, returning a temporary NodoDoble object, constructed within your RetornaUltimo() function at the return statement. Its scope (or lifetime) is limited and you can't rely on it still being there if you dereference a pointer to it in the future, so the compiler is (correctly) preventing you from doing it.

If you return a reference to the contained ultimo object, that's OK so long as you remember not to delete the objLista object until you've finished using the reference.

Also, your B2AB10Recursivo() method is changing the content of the provided NodoDoble object. If you don't return a reference from RetornaUltimo() you're not changing its content at all, you're changing some copy's content.

Cheers,
Jim
wow, it seems so simple but yet so complex, all I had to do was add one more &, cor blimey.
Thanks a lot Jim, the program works now!
No worries!
Topic archived. No new replies allowed.