2017-04-22 2 views
-3

私は大きなクラス(Collection)と小さなクラス(Item)を持っています。マップからオブジェクトにアクセスすると、新しいオブジェクトのインスタンスが生成されます。C++

Collectionオブジェクトを初期化すると、小さなオブジェクトへのマップのベクトルが作成されます。全体的な構造は次のようになります。それがために項目を強制だから私は項目にアクセスするためにコレクションのメソッドを呼び出すとき、すなわち

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

vector<map<int,Node>> layers; 

vector<map<int,Item>> storage; 

具体的には、のように見えます再初期化。ここでは、特定のコードです:

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がすでに作成されているときに、これらの初期化エラーが発生するのは本当に混乱しています。

答えて

2

layers[hidden_layer_num][node_index]のインデックス演算子を呼び出すと、新しいマップが作成されます(まだ存在していない場合)。node_indexがキーとして設定され、デフォルトの値が設定されます。既に存在するアイテムにアクセスするには、map::find()を使用する必要があります。

+0

既に存在すれば?マップは既に初期化されています – Gimingo

+4

@Gim std :: map _requires_のデフォルトのコンストラクタでoperator []を使用します。代わりにfind()を使用してください。 –

+0

既に存在する場合、マップに変更は加えられませんが、値型には依然としてデフォルトのコンストラクタが必要です。 – VTT

関連する問題