-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);

}
4

1 回答 1

5

是的,this只能在函数内使用。

最简单的更改是改用NULL默认值,然后NULL在函数中检查,然后this改用

于 2013-10-22T09:56:49.913 回答