c++ - 使用 std::map 和自定义类作为其值的编译器错误 C2664

标签 c++ visual-studio c++11 stdmap std-pair

我对这个问题进行了大量搜索,但没有一个答案有帮助。

我的设置

  • Visual Studio 2017
  • Windows 10

我有一个很大的代码库,在一个类 Bar 中


// Bar.h

class Bar {
public:
   Bar();
   ~Bar();

public:
    ... // a long list of other methods.

private:
    std::map<std::string, Foo> m_foos;

};

然后在执行中

// Bar.cpp

Bar::Bar() {
   m_foos.insert(std::make_pair(std::string("id"), Foo()));              // Error 1
   m_foos.insert({std::string("id"), Foo()});                            // Error 2
   m_foos.insert(std::pair<std::string, Foo>(std::string("id"), Foo())); // Error 3
}

尝试编译上面的代码给我:

错误 1

error C2664: 'std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>> std::_Tree<std::_Tmap_traits<_Kty,Foo,_Pr,_Alloc,false>>::insert(std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>,const std::pair<const _Kty,Foo> &)': cannot convert argument 1 from 'std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char>>,Foo>' to 'std::pair<const _Kty,_Ty> &&'

错误 2

error C2664: 'std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>> std::_Tree<std::_Tmap_traits<_Kty,Foo,_Pr,_Alloc,false>>::insert(std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>,const std::pair<const _Kty,Foo> &)': cannot convert argument 1 from 'initializer list' to 'std::pair<const _Kty,_Ty> &&'

错误 3

error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char>>,Foo>'

但是,使用相同的设置和编译器,以下大大简化的代码可以成功编译:

#include "pch.h"
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <map>
#include <string>
#include <vector>

class Foo {
public:
    Foo() { mi = 1; }
    ~Foo() {}

private:
    int mi;
};

class MyFoo : public Foo {
public:
    MyFoo() :Foo() {
        mj = 2;
        mk = nullptr;
    }
    ~MyFoo() {}

private:
    int mj;
    int* mk;
};

class YourFoo : public Foo {
public:
    YourFoo() :Foo() { mj = 2; }
    ~YourFoo() {}

private:
    int mj;
};

int main()
{
    std::map<int, Foo> yourmap;
    yourmap.insert(std::make_pair(3, Foo()));
    yourmap.insert({ 4, Foo() });
    yourmap.insert(std::pair<int, Foo>(5, Foo()));

}

我哪里错了?

更新

所以我终于可以重现它了。这是因为有问题的类层次结构删除了它们的复制构造函数和 operator =

修改后的例子可以重现错误。

#include "pch.h"
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <map>
#include <string>
#include <vector>

class Foo {
public:
    Foo() { mi = 1; }
    ~Foo() {}

    // NO COPIES.   
    Foo(const Foo &) = delete;
    Foo& operator = (const Foo &) = delete;

private:
    int mi;
};

class MyFoo : public Foo {
public:
    MyFoo() :Foo() {
        mj = 2;
        mk = nullptr;
    }
    ~MyFoo() {}

    // NO COPIES.
    MyFoo(const MyFoo &) = delete;
    MyFoo& operator = (const MyFoo &) = delete;

private:
    int mj;
    int* mk;
};

class YourFoo : public Foo {
public:
    YourFoo() :Foo() { mj = 2; }
    ~YourFoo() {}

    // NO COPIES.
    YourFoo(const YourFoo &) = delete;
    YourFoo& operator = (const YourFoo &) = delete;

private:
    int mj;
};


int main()
{
    std::map<int, Foo> yourmap;
    yourmap.insert(std::make_pair(3, Foo()));
    yourmap.insert({ 4, Foo() });
    yourmap.insert(std::pair<int, Foo>(5, Foo()));

}

所以我猜问题是 std::map 必须将元素复制到它的内存空间中。然后它只是碰到没有复制构造函数的类并提示。但是错误消息是如此具有误导性,以至于我无法弄清楚。实际的类层次结构非常庞大,所以我没有看到那些被删除的部分。我猜有人不希望这些东西被复制。

如果有人可以在我的问题开头指导如何破译编译器错误消息,以便我可以更好地了解在哪里查看,将不胜感激!

最佳答案

没有可用复制构造函数/复制赋值的类的实例仍然可以使用一些特殊技巧在 std::map 中使用:

  • emplace 而不是 insert(就地构建映射条目)
  • 使用 std::piecewise_construct

更多信息请访问 cppreference.com:std::map::emplace

应用于OP的示例代码:

#include <iostream>
#include <map>
#include <string>

class Foo {
public:
    Foo(): mi("1") { }
    ~Foo() = default;
    Foo(const Foo&) = delete;
    Foo& operator=(const Foo&) = delete;

private:
    std::string mi;
};

std::ostream& operator<<(std::ostream &out, const Foo &foo)
{
  return out << "Foo()";
}

int main()
{
  std::map<std::string, Foo> yourmap;
#if 0 // WON'T COMPILE due to deleted Foo::Foo(const Foo&):
  yourmap.insert(std::make_pair("3", Foo()));
  yourmap.insert({ "4", Foo() });
  yourmap.insert(std::pair<std::string, Foo>("5", Foo()));
#endif // 0
  yourmap.emplace(std::piecewise_construct,
    std::forward_as_tuple("3"), // std::string("3")
    std::forward_as_tuple()); // Foo()
  // check result
  for (auto &entry : yourmap) {
    std::cout << '"' << entry.first << "\": " << entry.second << '\n';
  }
}

输出:

"3": Foo()

Live Demo on coliru

关于c++ - 使用 std::map 和自定义类作为其值的编译器错误 C2664,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57967624/

相关文章:

c++ - 在特征库中的大矩阵中创建子矩阵

c# - 如何关闭Kinect USB口的句柄以防止VisualStudio死机?

visual-studio - 如何使用CMake生成具有32位和64位配置的VS proj

c++ - 在基类之间 move 语义

C++11:为什么 std::condition_variable 使用 std::unique_lock?

c++ - move 构造函数和 move 赋值运算符与复制省略

c++ - boost::unordered_map 中的运算符 []

c++ - 使用 MFC 创建 WM_USER 处理程序

visual-studio - 调试时转到 Visual Studio 中当前执行的语句

c++ - 在 std 命名空间中重载(非特化)模板