从映射访问对象会导致新对象 C++ 的实例化

Posted

技术标签:

【中文标题】从映射访问对象会导致新对象 C++ 的实例化【英文标题】:Accessing object from map causes instantiation of new object C++ 【发布时间】:2017-04-22 18:47:55 【问题描述】:

我有一个大类(称为 Collection)和一个小类(Item)。

在初始化 Collection 对象时,它会创建一个映射到小对象的向量。整体结构如下:

vector<map<int,Item>> storage; 

具体是这样的:

vector<map<int,Node>> layers;

所以当我调用 Collection 的方法来访问 Item 时,即

layers[vector_index][map_index].some_method();

它强制重新初始化项目。具体代码如下:

void NN::set_hidden_weights(int hidden_layer_num,int node_index,map<int,double> new_weights)
    layers[hidden_layer_num][node_index].set_weights(new_weights); //updates weights

我的意思是我的编译器出现以下错误:

 g++ -o tests.exe tests.cpp ../src/neural_network.cpp -std=c++11
In file included from /usr/include/c++/4.8/bits/stl_map.h:63:0,
                 from /usr/include/c++/4.8/map:61,
                 from ../src/neural_network.cpp:8:
/usr/include/c++/4.8/tuple: In instantiation of ‘std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>) [with _Args1 = const int&; long unsigned int ..._Indexes1 = 0ul; _Args2 = ; long unsigned int ..._Indexes2 = ; _T1 = const int; _T2 = Node]’:
/usr/include/c++/4.8/tuple:1079:63:   required from ‘std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = const int&; _Args2 = ; _T1 = const int; _T2 = Node]’
/usr/include/c++/4.8/bits/stl_tree.h:140:49:   required from ‘std::_Rb_tree_node<_Val>::_Rb_tree_node(_Args&& ...) [with _Args = const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>; _Val = std::pair<const int, Node>]’
/usr/include/c++/4.8/ext/new_allocator.h:120:4:   required from ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::_Rb_tree_node<std::pair<const int, Node> >; _Args = const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>; _Tp = std::_Rb_tree_node<std::pair<const int, Node> >]’
/usr/include/c++/4.8/bits/alloc_traits.h:254:4:   required from ‘static typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type std::allocator_traits<_Alloc>::_S_construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = std::_Rb_tree_node<std::pair<const int, Node> >; _Args = const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>; _Alloc = std::allocator<std::_Rb_tree_node<std::pair<const int, Node> > >; typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type = void]’
/usr/include/c++/4.8/bits/alloc_traits.h:393:57:   required from ‘static decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) std::allocator_traits<_Alloc>::construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = std::_Rb_tree_node<std::pair<const int, Node> >; _Args = const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>; _Alloc = std::allocator<std::_Rb_tree_node<std::pair<const int, Node> > >; decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) = <type error>]’
/usr/include/c++/4.8/bits/stl_tree.h:408:36:   required from ‘std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args&& ...) [with _Args = const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>; _Key = int; _Val = std::pair<const int, Node>; _KeyOfValue = std::_Select1st<std::pair<const int, Node> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, Node> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const int, Node> >*]’
/usr/include/c++/4.8/bits/stl_tree.h:1669:64:   required from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_emplace_hint_unique(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator, _Args&& ...) [with _Args = const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>; _Key = int; _Val = std::pair<const int, Node>; _KeyOfValue = std::_Select1st<std::pair<const int, Node> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, Node> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const int, Node> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<std::pair<const int, Node> >]’
/usr/include/c++/4.8/bits/stl_map.h:465:8:   required from ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = int; _Tp = Node; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, Node> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = Node; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = int]’
../src/neural_network.cpp:135:27:   required from here
/usr/include/c++/4.8/tuple:1090:70: error: no matching function for call to ‘Node::Node()’
         second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
                                                                      ^
/usr/include/c++/4.8/tuple:1090:70: note: candidates are:
../src/neural_network.cpp:12:1: note: Node::Node(int, std::vector<int>)
 Node::Node(int node_index, vector<int> input_indices)
 ^
../src/neural_network.cpp:12:1: note:   candidate expects 2 arguments, 0 provided
In file included from ../src/neural_network.cpp:9:0:
../src/neural_network.hpp:12:7: note: Node::Node(const Node&)
 class Node
       ^
../src/neural_network.hpp:12:7: note:   candidate expects 1 argument, 0 provided
../src/neural_network.hpp:12:7: note: Node::Node(Node&&)
../src/neural_network.hpp:12:7: note:   candidate expects 1 argument, 0 provided

我的 Item 类没有默认构造函数,也没有复制构造函数,无论如何,我真正想做的是操纵 Item 本身,而不是它的副本。有人对如何进行有任何建议吗?我真的很困惑为什么在已经创建对象 Item 时会出现这些初始化错误。

【问题讨论】:

【参考方案1】:

在映射layers[hidden_layer_num][node_index] 上调用索引运算符会导致构建一个新映射(如果它还没有),其中node_index 作为键和默认构造值。您应该使用map::find() 访问已经存在的项目。

【讨论】:

@Gim 在 std::map 上使用运算符 [] 需要 默认构造函数。请改用 find()。 如果已经存在,则不会对映射进行任何更改,但是值类型仍然需要具有默认构造函数。

以上是关于从映射访问对象会导致新对象 C++ 的实例化的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 QJSEngine 实例化 c++ 对象

c++实例化一个对象

从 JSON 实例化对象数组 [重复]

C++ 中,类的实例化对象是指啥?如果确保它的实例化对象只得一个?

C++初阶:类和对象(上篇)类的定义 | 类的访问限定符及封装 | 类的作用域 | 类的实例化 | 类对象模型 | this指针

实例化 GameObject 会导致所述对象的 Transform 被破坏?