gpt4 book ai didi

c++ - VS 2012 error C2679: binary '=' : 没有发现操作符正在执行 std::copy of map

转载 作者:行者123 更新时间:2023-11-28 05:24:10 24 4
gpt4 key购买 nike

<分区>

我找不到在 VS 2012 中编译以下简单 STL 代码的方法:

#include <algorithm>
#include <map>
#include <string>

...
class SomeClass;
SomeClass a;
std::map<std::string, SomeClass> x;
x["a"] = a;
std::map<std::string, SomeClass> y;
std::copy(x.begin(), x.end(), std::inserter(y, y.end()));

我总是遇到 VS 编译器错误:

xutility(2089): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::pair<_Ty1,_Ty2>' (or there is no acceptable conversion)

更新

真的很抱歉!我设法隔离了有问题的代码,就是这样。它基本上是智能指针 shared_ptr 的类型逆变问题,因为它无法复制 anAImplPtr = anAPtr 即使 anAPtr 实际上指向一个AImpl 实例的包装器:

#include <algorithm>
#include <map>
#include <string>
#include <iterator>
#include <boost/thread.hpp>

class A
{
public:
virtual ~A() { }
};

class AImpl : public A {
public:
virtual ~AImpl() { }
};

typedef boost::shared_ptr<A> APtr;
typedef boost::shared_ptr<AImpl> AImplPtr;

static void test()
{
std::map<std::string, APtr> x;
APtr a(new AImpl());
x["a"] = a;
std::map<std::string, AImplPtr> y;
std::copy(x.begin(), x.end(), std::inserter(y, y.end()));
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com