1

ブーストの次の例を使用しようとしています。

#include <boost/container/map.hpp>
struct data
{
    std::string label;
    //A map holding still undefined 'data'
    boost::container::map<std::string, data> m_;
};

int main(int argc, char* argv[])
{
    data d,d1,d2;
    d.m_["hello"] = d1;
    return 0;
}

残念ながら、コンパイルできず、その理由がわかりません。

コンパイラ メッセージ (ちょうど終わり): ../../3d_party_4_5_8/boost_1_49_0/boost/container/detail/tree.hpp|183| エラー: ג((boost::container::container_detail::rbtree_node, std::allocator >, data>, void*>*)this)->boost::container::container_detail:: の גoperator=ג に一致しませんrbtree_node, std::allocator >, data>, void*>::m_data.boost::container::container_detail::pair, std::allocator >, data>::second = p->boost::container:: container_detail::pair, std::allocator >, data>::secondAnalyzer.cpp|139| 注: 候補は: data& data::operator=(data&)

4

2 に答える 2

2

あなたは C++03 モードでコンパイルしていると思います。エラーメッセージに示されているように、代入演算子は定数ではありませんdata& data::operator=(data&): 、これが問題です。boost::containerC++03 コンパイラのムーブ セマンティクスをエミュレートし、これを行うために非 const コピー コンストラクタを定義します。考えられる解決策の 1 つは、以下を に追加することdataです。

data& operator=(data x)
{
    std::swap(*this, x); 
    return *this;
}

また

data& operator=(const data& x)
{
    label = x.label 
    return *this;
}

あなたのニーズに応じて。

于 2013-01-27T20:27:52.357 に答える
1

それが可能だとは思っていませんでした。しかし、それは確かです。挿入するためにこれを試してください:

//...
int main(int argc, char* argv[])
{
    data d,d1,d2;
    d.m_.insert( std::make_pair(std::string("hello"), d1) ) ;
    return 0;
}
// ...

... C++03 でも動作します。

于 2013-01-27T20:27:55.733 に答える