【问题标题】:Cannot compile: error: expected primary-expression before '(' token无法编译:错误:“(”标记之前的预期主表达式
【发布时间】:2021-01-30 20:21:27
【问题描述】:

我无法编译:

// 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&lt;typename Container::index&lt;by_attrs&gt;::type::iterator, typename Container::index&lt;by_attrs&gt;::type::iterator&gt; MultiIndex&lt;Container&gt;::fetch_range(FromArgs&amp;&amp;, ToArgs&amp;&amp;) const': main.cpp:28:55: error: expected primary-expression before '(' token 28 | return std::pair&lt;attr_iterator, attr_iterator&gt;(

【问题讨论】:

  • 它不会喜欢Container::templatetemplate 是关键字,不能与作用域解析运算符一起使用
  • @Bathsheba 显然是需要的。华金解决了这个问题。
  • 好吧,我并没有正式过时。对这个问题投赞成票 - 明显高于我的工资等级。

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


【解决方案1】:

你需要在这里放几个templates:

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?

【讨论】:

  • 先生,您真是个天才,谢谢!并感谢您对 boost 的贡献。
猜你喜欢
  • 2018-06-28
  • 2011-03-31
  • 2013-01-13
  • 2017-09-24
  • 2012-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多