c++ - 无法编译: error: expected primary-expression before '(' token

标签 c++ c++11 templates compiler-errors boost-multi-index

我无法进行此编译:

// 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<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?

关于c++ - 无法编译: error: expected primary-expression before '(' token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64386996/

相关文章:

c++ - 可以基于范围的 C++11 执行/检查额外的操作/条件吗?

C++ 访问控制不适用于模板化对象

c++ - 可自定义常量的基类参数与纯虚函数

c++ - 是否因为以下原因在堆中实例化一个对象?

c++ - "What happened to my SFINAE"还原 : conditional template class members?

c++ - 模板特化在其实例化点看不到函数

templates - 用于模板化重载的不明确运算符<<

c++ - 仅释放分配给 "operator new"的一部分内存

c++ - 尝试使用函数 c++ 创建一个骰子游戏

c++ - 在 C++ 中的其他类中设置模板参数