-3

I want to write a recursive function that gives me the lenght of a list. To make it recursive, i use this pointer as default parameter in the declaration of the function in the List class header. But the compiler gives me an error... This is the code:

//Header file
#include "Nodo.h"

template < class Tipo >

class Lista
{

    private:
        Nodo< Tipo >* Prox; 

    public:
        Lista();

        bool ListaVuota();

        int DimensioneLista(Lista<Tipo>* = this);

        void InserisciInCoda(Tipo);

};

//CPP file

template< class Tipo >

int Lista< Tipo >::DimensioneLista(Lista< Tipo >* lista)

{

    if(lista->ListaVuota())
        return 0;
    else
        return 1+DimensioneLista(lista);

}
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479

1 Answers1

5

Yes, this can only be used within functions.

The simplest change would be to use NULL as your default value instead, then check for NULL in your function, and use this instead

benjymous
  • 2,102
  • 1
  • 14
  • 21