传递模板参数时MSVC ++编译器错误C2923

时间:2012-09-23 18:56:50

标签: c++ visual-studio-2010 templates boost boost-multi-index

我有multi_index_container struct Person

struct Person {
    std::string firstName;
    std::string lastName;
    int id;
    bool operator <(const Person& rhs) const {return id < rhs.id;}
};

//index tags
struct BaseIndexTag {};
struct FirstName : BaseIndexTag {};
struct LastName : BaseIndexTag {};
struct ID : BaseIndexTag {};

namespace bmi = boost::multi_index;

typedef boost::multi_index_container<
    Person, 
    bmi::indexed_by<
    //PinsConnection operator < ordering
    bmi::ordered_unique<bmi::tag<ID>, bmi::identity<Person> >,
    //order by first name
    bmi::ordered_non_unique<bmi::tag<FirstName>, bmi::member<Person, std::string, &Person::firstName> >,
    //Order by last name
    bmi::ordered_non_unique<bmi::tag<LastName>, bmi::member<Person, std::string, &Person::lastName> >
    >
> PersonMultiSet; 

和boost iterator_adaptor的包装类:

template <
typename I      //Internal iterator type
, typename C    //Container class
>   
class IteratorAdaptor : public boost::iterator_adaptor <
    IteratorAdaptor<I,C>,               //Derived
    I,                                  //Base      
    boost::use_default,                 //Value
    boost::forward_traversal_tag>       //Traversal
{
private:
    friend class boost::iterator_core_access;
    friend C;
public:
    IteratorAdaptor()
        : typename IteratorAdaptor<I,C>::iterator_adaptor_() {}
    explicit IteratorAdaptor(I& b)
        : typename IteratorAdaptor<I,C>::iterator_adaptor_(b) {}
};

最后,我为PersonMultiSet编写了一个包装类,以便为这个类的用户公开一个干净的接口,并避免使用boost multi_index_container有点麻烦的接口:

class PersonContainer {
public:
    typedef IteratorAdaptor<PersonMultiSet::index<ID>::type::iterator, PersonContainer> IDIterator;
    typedef IteratorAdaptor<PersonMultiSet::index<ID>::type::const_iterator, PersonContainer> IDConstIterator;
    typedef IteratorAdaptor<PersonMultiSet::index<FirstName>::type::iterator, PersonContainer> FirstNameIterator;
    typedef IteratorAdaptor<PersonMultiSet::index<FirstName>::type::const_iterator, PersonContainer> FirstNameConstIterator;
    typedef IteratorAdaptor<PersonMultiSet::index<LastName>::type::iterator, PersonContainer> LastNameIterator;
    typedef IteratorAdaptor<PersonMultiSet::index<LastName>::type::const_iterator, PersonContainer> LastNameConstIterator;
    template <typename T> //tag
    void foo(IteratorAdaptor<PersonMultiSet::index<T>::type::iterator, PersonContainer> &it)
    {
        //...
    }
private:
    PersonMultiSet people;
};

在VS2010下编译此代码会对函数foo的声明产生以下错误:

1>  main.cpp
1>main.cpp(70): warning C4346: 'boost::multi_index::multi_index_container<Value,IndexSpecifierList>::index<T>::boost::multi_index::multi_index_container<Value1,IndexSpecifierList1,Allocator1>::index<Tag>::type::iterator' : dependent name is not a type
1>          with
1>          [
1>              Value=Person,
1>              IndexSpecifierList=boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::tag<ID>,boost::multi_index::identity<Person>>,boost::multi_index::ordered_non_unique<boost::multi_index::tag<FirstName>,boost::multi_index::member<Person,std::string,pointer-to-member(0x0)>>,boost::multi_index::ordered_non_unique<boost::multi_index::tag<LastName>,boost::multi_index::member<Person,std::string,pointer-to-member(0x20)>>>
1>          ]
1>          prefix with 'typename' to indicate a type
1>main.cpp(70): error C2923: 'IteratorAdaptor' : 'boost::multi_index::multi_index_container<Value,IndexSpecifierList>::index<T>::boost::multi_index::multi_index_container<Value1,IndexSpecifierList1,Allocator1>::index<Tag>::type::iterator' is not a valid template type argument for parameter 'I'
1>          with
1>          [
1>              Value=Person,
1>              IndexSpecifierList=boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::tag<ID>,boost::multi_index::identity<Person>>,boost::multi_index::ordered_non_unique<boost::multi_index::tag<FirstName>,boost::multi_index::member<Person,std::string,pointer-to-member(0x0)>>,boost::multi_index::ordered_non_unique<boost::multi_index::tag<LastName>,boost::multi_index::member<Person,std::string,pointer-to-member(0x20)>>>
1>          ]
1>
1>Build FAILED.

似乎编译器不允许在第一次使用时确定类型IteratorAdaptor<PersonMultiSet::index<T>::type::iterator(在我尝试实例化PersonContainer或调用foo之前我得到此错误)。 我做错了什么?

1 个答案:

答案 0 :(得分:3)

编译器会给你一个提示错误的信息:

  

前缀'typename'表示类型

因此,您需要在“有问题”参数之前添加typename

void foo(IteratorAdaptor<PersonMultiSet::index<T>::type::iterator, 
                         PersonContainer> &it)

必须是:

//                         vvvvvvvv
void foo( IteratorAdaptor< typename PersonMultiSet::index<T>::type::iterator,
                           PersonContainer> &it)