无法创建对-构造函数

博杰克

class Edge我知道对中的一个类是由于Edge中的构造函数时,我无法创建对,但是我不知道这是什么错误。

Edge构造函数有一个,Token因为我想确保只有类型为Object的对象Vertex才能创建对象Edge

class Edge
{
public:
   class ConstructionToken
   {
   private:
      ConstructionToken();
      friend class Vertex;
   };

   Edge( const Edge &) = default;
   Edge( const ConstructionToken & ){};

private
   //weight, etc...
};

void
Vertex::insert_edge( const std::string & end_point )
{
   Edge new_edge( Edge::ConstructionToken );
   std::pair<std::string,Edge> temp( end_point, new_edge );
   //edges.insert( temp );


}

编译错误

lib/Vertex.cpp:12:32: error: no matching constructor for initialization of 'std::pair<std::string, Edge>'
   std::pair<std::string,Edge> temp( end_point, new_edge );
                               ^     ~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:262:5: note: candidate constructor not viable: no known conversion
      from 'Edge (Edge::ConstructionToken)' to 'const Edge' for 2nd argument
    pair(const _T1& __x, const _T2& __y)
迈克·西摩

Edge new_edge( Edge::ConstructionToken );

声明函数,因为括号中的名称是类型。要声明变量,请使用

Edge new_edge{ Edge::ConstructionToken{} };  // C++11 or later
Edge new_edge((Edge::ConstructionToken())); // Historic dialects

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章