9

vorrei definire alcuni metodi di membro template all'interno di una classe template in questo modo:errore "troppi modello-parametro-liste" quando specializzata una funzione di membro

template <typename T> 
class CallSometing { 
public: 
    void call (T tObj); // 1st 

    template <typename A> 
    void call (T tObj, A aObj); // 2nd 

    template <typename A> 
    template <typename B> 
void call (T tObj, A aObj, B bObj); // 3rd 

}; 


template <typename T> void 
CallSometing<T>::call (T tObj) { 
    std::cout << tObj << ", " << std::endl; 
} 

template <typename T> 
template <typename A> void 
CallSometing<T>::call (T tObj, A aObj) { 
    std::cout << tObj << ", " << aObj << std::endl; 
} 


template <typename T> 
template <typename A> 
template <typename B> void 
CallSometing<T>::call (T tObj, A aObj, B bObj) { 
    std::cout << tObj << ", " << aObj << ", " << bObj << ", " << std::endl; 
} 

Ma quando instantializing classe template, non v'è un errore riguardante la definizione del menthod a tre argomenti:

CallSometing<int> caller; 

caller.call(12); // OK 
caller.call(12, 13.0); // OK 
caller.call (12, 13.0, std::string("lalala!")); // NOK - complains "error: too many template-parameter-lists" 

Potrebbe per favore indicare cosa sto facendo male? Perché il (2) metodo è OK ma il (3) causa un errore di compilazione?

risposta

17

Leggere un tutorial modello C++ su come assegnare a un modello più parametri. Invece di

template<typename A> template<typename B> void f(A a, B b); 

Il modo in cui è fatto è

template<typename A, typename B> void f(A a, B b); 

clausole template multipli rappresentano più livelli di template (modello di classe -> template membro).

+1

Aghhhhh, +1 per essere quattro secondi più veloce. :-D –

+0

@James: Non era quattro secondi più tardi, era tre secondi prima. ':)' – sbi

Problemi correlati