Reputation: 479
I can not create a pair when one of the classes in the pair in a class Edge
I know it is because of the constructors in Edge, but I do not know what is wrong.
The Edge constructor has a Token
because I wanted to make sure that only an Object of type Vertex
can create an object 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 );
}
Compiling error
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)
Upvotes: 0
Views: 3134
Reputation: 254431
This
Edge new_edge( Edge::ConstructionToken );
declares a function, since the name in parentheses is a type. To declare a variable, use
Edge new_edge{ Edge::ConstructionToken{} }; // C++11 or later
Edge new_edge((Edge::ConstructionToken())); // Historic dialects
Upvotes: 4