【问题标题】:using boost multi_index within a templated structure在模板结构中使用 boost multi_index
【发布时间】:2015-07-12 17:45:17
【问题描述】:

当我尝试编译此代码时,我在第 59 行收到 error: expected unqualified-id before 'typename'

#include <cstdlib>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>


template <typename ValueT, typename SizeT = size_t>
struct Test {
    typedef SizeT SizeType;
    typedef ValueT ValueType;
    struct ValueTag {};
    struct ProbabilityTag {};
    struct TotalProbabilityTag {};
    struct NodeType {
        SizeType value;
        ValueType probability;
        ValueType totalProbability;

        typedef boost::multi_index_container<
            NodeType,
            boost::multi_index::indexed_by<
                boost::multi_index::ordered_unique<
                    boost::multi_index::tag<ValueTag>,
                    BOOST_MULTI_INDEX_MEMBER(NodeType, SizeType, value)
                >,
                boost::multi_index::ordered_non_unique<
                    boost::multi_index::tag<ProbabilityTag>,
                    BOOST_MULTI_INDEX_MEMBER(NodeType, ValueType, probability)
                >,
                boost::multi_index::ordered_non_unique<
                    boost::multi_index::tag<TotalProbabilityTag>,
                    BOOST_MULTI_INDEX_MEMBER(NodeType, ValueType, totalProbability)
                >
            > 
        > NodeIndexType;

        NodeIndexType * node;
        NodeType(const NodeType & o):
            value(o.value),
            probability(o.probability),
            totalProbability(o.totalProbability)
        {
            if (o.node != NULL) {
                this->node = new NodeIndexType(*(o.node));
            }
        }

        ~NodeType() {
            delete this->node;
        }

        bool operator< (const NodeType & b) const {
            return this->value < b.value;
        }
    };

    typedef typename NodeType::NodeIndexType NodeIndexType;
    typedef NodeIndexType::typename index<typename ValueTag>::type ViewNodeByValue; /* this does not work */
};


int main() {
    Test<int> a;

    return EXIT_SUCCESS;
}

如果我删除注释行,一切正常。我已经看过template parameter to boost multi index container,但我找不到在模板结构或类中让它工作的方法。

谁能告诉我如何解决这个问题?

有关错误消息,请参阅http://codepad.org/UPMapaXI

【问题讨论】:

    标签: templates boost-multi-index


    【解决方案1】:

    运行更多测试并再次查看相关问题很明显,这样做的正确方法是

    typedef typename NodeIndexType::template index<ValueTag>::type NodeByValue;
    

    正如template parameter to boost multi index container 中已经解释的那样。重点是将索引标记为模板。

    另一种方法是使用

    typedef typename boost::multi_index::index<NodeIndexType, ValueTag>::type ViewNodeByValue;
    

    我在boost multi index container of template-dependent struct in template-class找到的。

    【讨论】:

    • 我遇到了同样的问题,gcc 4.8.5 给了我建议:CollectionManager.hpp:50:32: 注意:使用 CollectionManager::CollectionContainer::template index 来表明它是一个模板
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    • 2016-03-23
    • 2011-03-29
    • 2017-01-27
    • 1970-01-01
    相关资源
    最近更新 更多