【问题标题】:boost::multi_index error "template argument 'x' is invalid"boost::multi_index 错误“模板参数‘x’无效”
【发布时间】:2013-07-31 16:58:44
【问题描述】:

所以我用 boost_multi_index 制作了某种数据库,如下所示:

#include <boost\multi_index_container.hpp>
#include <boost\multi_index\ordered_index.hpp>
#include <boost\multi_index\member.hpp>
using namespace boost::multi_index;
using namespace std;

struct list_entry{
    int id;
    string name;
    string* data;
};

typedef multi_index_container<list_entry,
    indexed_by<
        ordered_unique< tag<int>, member<list_entry, int, &list_entry::id>>,                // unique id
        ordered_unique< tag<string>, member<list_entry, string, &list_entry::name>>,        // unique name
        ordered_non_unique< tag<string*>, member<list_entry, string*, &list_entry::data>>   // some data associated with the id/name
    >
>table;

class Database
{
    private:    
        table music, names;
        typedef table::index<int>::type list_id;
        typedef table::index<string>::type list_string;

    //some more code here
};

它可以在 Visual Studio 2010 中正常编译。

但是我想用 MinGW 将我的项目切换到 Code::Blocks,他似乎对此不太满意,这是编译日志:

.\source\db.h|19|error: template argument 3 is invalid|
.\source\db.h|15|error: template argument 2 is invalid|
.\source\db.h|15|error: template argument 1 is invalid|
.\source\db.h|14|error: template argument 2 is invalid|
.\source\db.h|13|warning: 'typedef' was ignored in this declaration [enabled by default]|
.\source\db.h|24|error: 'table' does not name a type|
.\source\db.h|25|error: 'table' does not name a type|
.\source\db.h|26|error: 'table' does not name a type|
C:\Program Files\boost_1_54_0\boost\system\error_code.hpp|222|warning: 'boost::system::posix_category' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\system\error_code.hpp|223|warning: 'boost::system::errno_ecat' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\system\error_code.hpp|224|warning: 'boost::system::native_ecat' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\asio\error.hpp|244|warning: 'boost::asio::error::system_category' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\asio\error.hpp|246|warning: 'boost::asio::error::netdb_category' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\asio\error.hpp|248|warning: 'boost::asio::error::addrinfo_category' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\asio\error.hpp|250|warning: 'boost::asio::error::misc_category' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\asio\ssl\error.hpp|34|warning: 'boost::asio::error::ssl_category' defined but not used [-Wunused-variable]|
C:\Program Files\boost_1_54_0\boost\asio\detail\winsock_init.hpp|116|warning: 'boost::asio::detail::winsock_init_instance' defined but not used [-Wunused-variable]|
||=== Build finished: 7 errors, 10 warnings (0 minutes, 15 seconds) ===|

我完全一无所知,因为错误没有比这更具体,而且我找不到任何相关问题。

所以我希望有人可以在这里给我一个答案,如果需要更多信息,我会在以后编辑它。

【问题讨论】:

    标签: c++ cross-platform boost-multi-index


    【解决方案1】:

    我可以像这样使用 BOOST_MULTI_INDEX_MEMBER 来解决这个问题:

    typedef multi_index_container<list_entry,
        indexed_by<
            ordered_unique< tag<int>, BOOST_MULTI_INDEX_MEMBER(list_entry, int, id)>,
            ordered_unique< tag<string>, BOOST_MULTI_INDEX_MEMBER(list_entry, string, name)>,
            ordered_non_unique< tag<string*>, BOOST_MULTI_INDEX_MEMBER(list_entry, string*, data)>
        >
    >table;
    

    【讨论】:

    • 这很奇怪...我怀疑member 存在名称冲突问题,您可以尝试像::boost::multi_index::member 一样明确限定
    • 我之前尝试过将所有内容明确限定为boost::multi_index::(没有前导::)并删除using namespace 行,但问题仍然存在。然而,在进行了一些更广泛的搜索后,我发现了 BOOST_MULTI_INDEX_MEMBER,它似乎在编写跨平台代码时使用,所以我决定尝试一下(就像我说的,它有效)。
    • 嗯,BOOST_MULTI_INDEX_MEMBER 会产生影响的唯一情况是,如果您的编译器太旧以至于启用了BOOST_NO_POINTER_TO_MEMBER_TEMPLATE_PARAMETERS 缺陷宏。你能检查一下吗?
    • ...哇,好像那时我没有为编译器使用 -std=c++11 参数。如果我没记错的话,我解决了上述问题,然后我的代码遇到了其他问题,然后我发现参数没有设置。所以,是的,原始代码在缺少参数的情况下编译得很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多