2010-05-28 38 views
13

Tôi đang gặp rắc rối với các mẫu và các loại phụ thuộc: khiếu nạiRắc rối với các loại phụ thuộc vào mẫu

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

Compiler, từ vị trí đã nêu ở trên:

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' 

Tôi đang làm gì sai?

+1

Trong VS2017, đây là C7510. –

Trả lời

27

Vâng, cảnh báo nói:

tên phụ thuộc không phải là một loại. tiền tố có 'typename' để chỉ ra một loại

Tên phụ thuộc (có nghĩa là, iterator) không phải là một loại. Bạn cần phải thêm tiền tố nó với typename để chỉ một loại:

typename std::set<result_t>::iterator 

Vì vậy, tuyên bố của bạn nên là:

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^

(và định nghĩa phải phù hợp với tờ khai)

4

Bạn cần có thêm từ khóa tên tệp trên dòng này:

set<result_t> findAll_if(typename set<result_t>::iterator begin,tên tệpset<result_t>::iterator end, Predicate pred) // warning C4346

Các vấn đề liên quan