微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法编译:错误:''标记之前的预期主表达式

如何解决无法编译:错误:''标记之前的预期主表达式

我无法获得此编译:

// main.cpp

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/indexed_by.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/tag.hpp>

using namespace boost::multi_index;


struct by_attrs{};

// Generic MultiIndex that wraps boost::multi_index
template<typename Container>
class MultiIndex
{
public:

    typedef typename Container::template index<by_attrs>::type::iterator attr_iterator;


    template<typename FromArgs,typename ToArgs>
    std::pair<attr_iterator,attr_iterator>
    fetch_range(FromArgs&& from,ToArgs&& to)
    const
    {  
        return std::pair<attr_iterator,attr_iterator>(
                _container.get<by_attrs>().lower_bound(from),_container.get<by_attrs>().upper_bound(to)
        );
    }  

private:

    Container _container;
};


class Foo
{
public:
    int bar() const
    {  
        return 1; 
    }  
};


typedef multi_index_container<
    Foo,indexed_by<
        ordered_unique<
            tag<by_attrs>,composite_key<
                Foo,const_mem_fun<
                    Foo,int,&Foo::bar
                >
            >
        >
    >  
> FooMultiIndexContainer;


typedef MultiIndex<FooMultiIndexContainer> FooMultiIndex;


int main()
{
    FooMultiIndex foo_index;
}

错误g++ -std=c++11 main.cpp):

In member function 'std::pair<typename Container::index<by_attrs>::type::iterator,typename Container::index<by_attrs>::type::iterator> MultiIndex<Container>::fetch_range(FromArgs&&,ToArgs&&) const': main.cpp:28:55: error: expected primary-expression before '(' token 28 | return std::pair<attr_iterator,attr_iterator>(

解决方法

您需要在此处放置几个template

template<typename FromArgs,typename ToArgs>
std::pair<attr_iterator,attr_iterator>
fetch_range(FromArgs&& from,ToArgs&& to)
const
{  
    return std::pair<attr_iterator,attr_iterator>(
            _container.template get<by_attrs>().lower_bound(from),_container.template get<by_attrs>().upper_bound(to)
    );
}  

您可能希望在所谓的相关上下文中使用typenametemplate来咨询该线程:Where and why do I have to put the “template” and “typename” keywords?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。