首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >传递模板参数时MSVC++编译器出现错误C2923

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

Stack Overflow用户
提问于 2012-09-24 02:56:50
回答 1查看 1.8K关注 0票数 1

我有一个struct Personmulti_index_container

代码语言:javascript
复制
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的包装类

代码语言:javascript
复制
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接口有些笨重:

代码语言:javascript
复制
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的声明上产生以下错误:

代码语言:javascript
复制
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之前就得到了这个错误)。我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-24 03:09:47

编译器会提示你出了什么问题:

前缀“”typename“”表示类型

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

代码语言:javascript
复制
void foo(IteratorAdaptor<PersonMultiSet::index<T>::type::iterator, 
                         PersonContainer> &it)

必须是:

代码语言:javascript
复制
//                         vvvvvvvv
void foo( IteratorAdaptor< typename PersonMultiSet::index<T>::type::iterator,
                           PersonContainer> &it)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12555300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档