2013-04-18 3 views
1

У меня ситуация как этотаргумент шаблона для функции в шаблон класса

template<class T> class Vector { 
    T *data; 
    uint _size, _counter; 
public: 
    class Iterator; 
    template<template<class> class C> Vector(typename C<T>::Iterator it1, 
              typename C<T>::Iterator it2) { 
    data = NULL; 
    _size = _counter = 0; 
    for(typename C<T>::Iterator it = it1; it != it2 && it != end(); it++) 
     push(*it); 
    } 
}; 

, что это мой собственный вектор класс и поведение конструктора подражает вектора (и можно построить его с диапазоном данных, предоставленных с использованием interators), но добавьте требование о том, чтобы контейнер был шаблоном того же типа, что и тот, который строится. Я получаю ошибку

5.cpp:16:36: error: no matching function for call to ‘Vector::Vector(Vector::Iterator, Vector::Iterator)’ 5.cpp:16:36: note: candidates are: In file included from 5.cpp:2:0:

5.hpp:17:37: note: template class typedef C C> Vector::Vector(typename C::Iterator, typename C::Iterator)

5.hpp:17:37: note: template argument deduction/substitution failed:

5.cpp:16:36: note: couldn't deduce template parameter ‘template class typedef C C’ In file included from 5.cpp:2:0:

5.hpp:11:3: note: Vector::Vector() [with T = int]

5.hpp:11:3: note: candidate expects 0 arguments, 2 provided

5.hpp:7:25: note: Vector::Vector(const Vector&)

5.hpp:7:25: note: candidate expects 1 argument, 2 provided

Нужна помощь здесь.

+0

Что, что 'шаблон <шаблон класс C>'? –

+0

конструктор - это шаблонный метод с параметром C, который сам по себе является шаблоном с одним именем параметра unvelant. –

+0

'it! = End()'. Это не так. – jrok

ответ

3

В:

template<template<class> class C> Vector(typename C<T>::Iterator it1, 
              typename C<T>::Iterator it2) 

компилятор не выводит тип C из typename C<T>::Iterator, потому что это то, что называется nondeduced контекст.

См §14.8.2.4 Выведение аргументы шаблона из типа [temp.deduct.type]:

4 The nondeduced contexts are:

— The nested-name-specifier of a type that was specified using a qualified-id.

— A type that is a template-id in which one or more of the template-arguments is an expression that references a template-parameter.

Смежные вопросы