2017-04-04 20 views
-2

これはおそらく簡単な質問です。しかし、マップにオブジェクトを挿入することはできません。私は私が自分のコピーコンストラクタを定義する必要はないと確信しています。今、壁に頭を強打ように感じる:頂点のstd :: mapにデフォルト以外の構成要素を挿入する

map<unsigned, Vertex> vertexes; 

ヘッダファイル:私は呼び出ししようとしています

#ifndef KNOTSV2_VERTEX_H 
    #define KNOTSV2_VERTEX_H 

    #include <forward_list> 
    #include <set> 
    #include <iostream> 

    #include "VertexType.h" 

    using namespace std; 

    // Defines. 
    typedef tuple<edge_list, edge_list, edge_list> neigh_set_entry; 


    /* 
    * Matching 
    */ 
    struct match_first { 
     match_first(unsigned value) : value(value) {}; 

     template<class A> 
     bool operator()(const pair<unsigned, A> &a) { 
      return a.first == value; 
     } 
    private: 
     unsigned value; 
    }; 


    class Vertex { 
    public: 
     /* 
     * Constructors 
     */ 
     Vertex(unsigned identifier, VertexType *type, unsigned attributes) : identifier(identifier), type(type), values(attributes_vec(attributes)) {}; 

// Methods .. 
    private: 
     /* 
     * Members 
     */ 
     unsigned identifier; 
     attributes_vec values; 
     VertexType *type; 
     vector<pair<unsigned, neigh_set_entry>> neigh_set; 
    }; 

機能:笑:,これは私が作成しようとしていますマップである

Vertex& Graph::create_vertex(VertexType &type) { 
    Vertex v(id_enumerator++, &type, type.num_attributes()); 
    return vertexes[id_enumerator] = v; 
} 

ノート:メンバ関数のインスタンス化の 'STD :: __ 1つの::マップ、のstd :: __ 1 ::アロケータ>> ::演算子[]' ここ

を要求します
return vertexes[id_enumerator] = v; 

      ^

ノート:候補コンストラクタ(暗黙のムーブコンストラクタ)ない生きは:1つの引数が必要ですが、0を

class Vertex { 
    ^

ノート提供された:候補コンストラクタ(暗黙のコピーコンストラクタ)は実行可能ではありません:

注:実行可能でない候補コンストラクタは3つの引数を必要としますが、0は提供される 頂点(符号なし識別子、VertexType *型、符号なし属性):識別子(識別子)、型(型)、値(属性_vec(属性)){};

私はそれが '普通のコンストラクタ'を呼び出そうとしていることが原因だと理解していますが、値のゲッタとセッタをコンストラクタに渡したいとは思いません。彼らは変更されないかもしれないので。 2回目の試みとして、map :: emplaceを使用しようとしましたが、そこには運がありません。通常のコンストラクタを作成する以外に他のオプションはありますか?

試みられたemplaceとinsertとpairwiseの構造で、どちらも運がありません。

Vertex& Graph::create_vertex(VertexType &type) { 
    //Vertex v(); 
    return vertexes.insert(make_pair(id_enumerator, Vertex(id_enumerator, &type, type.num_attributes()))).first->second; 
} 

Vertex& Graph::create_vertex(VertexType &type) { 
    //Vertex v(); 
    return vertexes.emplace(id_enumerator, Vertex(id_enumerator, &type, type.num_attributes())).first->second; 
} 
Vertex& Graph::create_vertex(VertexType &type) { 
    //Vertex v(); 
    return vertexes.emplace(piecewise_construct, forward_as_tuple(id_enumerator), forward_as_tuple(id_enumerator, &type, type.num_attributes())).first->second; 
} 
+0

? – NathanOliver

答えて

2

読むstd::map::emplace's documentation

新しい要素(すなわちstd::pair<const Key, T>)のコンストラクタがstd::forward<Args>(args)...を介して転送され、据え付けるために供給されるとまったく同じ引数で呼び出されます。 `insert`と` emplace`をしようとしたときにエラーがあなたが得る何をすべきか、コンパイラ

struct Vertex 
{ 
    Vertex(unsigned identifier, int type, unsigned attributes) {} 
}; 

int main() 
{ 
    std::map<unsigned, Vertex> vertexes; 

//      Vertex 
//      vvvvvvvvvvvvvvvvv 
    vertexes.emplace(0u, Vertex{0u, 1, 0u}); 
//     ^^ 
//  const unsigned 
} 

live wandbox example

関連する問題