1

I try to use the following example from boost:

#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;
}

unfortunately it does not compile and I can't figure why.

Compilers Message (just the end of it): ../../3d_party_4_5_8/boost_1_49_0/boost/container/detail/tree.hpp|183| error: no match for גoperator=ג in ג((boost::container::container_detail::rbtree_node, std::allocator >, data>, void*>*)this)->boost::container::container_detail::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>::secondג Analyzer.cpp|139| note: candidates are: data& data::operator=(data&)

5
  • 3
    Did it possibly occur to you that it might be helpful to include the compiler error message in your question?!
    – us2012
    Jan 27, 2013 at 20:21
  • Did you #include <string>?
    – Kerrek SB
    Jan 27, 2013 at 20:24
  • 1
    Are you compiling in C++03 mode?
    – Jesse Good
    Jan 27, 2013 at 20:26
  • Yes, I can't use c++11, but I don't see c++11 here or am I missing something?
    – Ezra
    Jan 27, 2013 at 20:28
  • 1
    @Ezra: boost::container uses move emulation in C++03 mode, this is one of the limitations to it (see link in my answer).
    – Jesse Good
    Jan 27, 2013 at 20:34

2 Answers 2

2

I think you are compiling in C++03 mode: Please see Assignment operator in classes derived from or holding copyable and movable types. Your assignment operator is non-const as shown in the error message: data& data::operator=(data&), this is the problem. boost::container emulates move semantics in C++03 compilers, and in order to do this defines a non-const copy constructor. One possible solution is too add the following to data:

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

or

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

depending on your needs.

1

I never thought that that were possible. But it is indeed. Try this for inserting:

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

... it works even in C++03.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.