2010-05-28 14 views
13

Ho problemi con i modelli e tipi dipendenti: reclamiProblemi con tipi dipendenti nei modelli

namespace Utils 
{ 
    void PrintLine(const string& line, int tabLevel = 0); 
    string getTabs(int tabLevel); 

    template<class result_t, class Predicate> 
    set<result_t> findAll_if(typename set<result_t>::iterator begin, set<result_t>::iterator end, Predicate pred) // warning C4346 
    { 
     set<result_t> result; 
     return findAll_if_rec(begin, end, pred, result); 
    } 
} 

namespace detail 
{ 
    template<class result_t, class Predicate> 
    set<result_t> findAll_if_rec(set<result_t>::iterator begin, set<result_t>::iterator end, Predicate pred, set<result_t> result) 
    { 
     typename set<result_t>::iterator nextResultElem = find_if(begin, end, pred); 
     if (nextResultElem == end) 
     { 
      return result; 
     } 
     result.add(*nextResultElem); 

     return findAll_if_rec(++nextResultElem, end, pred, result); 
    } 
} 

compilatore, dalla posizione osservato in precedenza:

warning C4346: 'std::set<result_t>::iterator' : dependent name is not a type. prefix with 'typename' to indicate a type 
error C2061: syntax error : identifier 'iterator' 

Che cosa sto facendo di sbagliato?

+1

In VS2017, questo è ora C7510. –

risposta

27

Ebbene, l'avviso dice:

nome dipendente non è un tipo. prefisso con 'nometipo' per indicare un tipo

Il nome dipendente (cioè, il iterator in std::set<result_t>::iterator) non è un tipo. È necessario premettere ad essa typename per indicare un tipo:

typename std::set<result_t>::iterator 

Così, il vostro dichiarazione deve essere:

template<class result_t, class Predicate> 
set<result_t> findAll_if(typename set<result_t>::iterator begin, typename set<result_t>::iterator end, Predicate pred) 
               note added typename^

(e la definizione dovrebbe corrispondere alla dichiarazione)

4

È necessario un ulteriore typename keyword su questa riga:

set<result_t> findAll_if(typename set<result_t>::iterator begin,typenameset<result_t>::iterator end, Predicate pred) // warning C4346