Error when coding a graph class in C++ -
i know debugging other people's code can frustrating want find out what's wrong mine. it's incomplete graph lot of functions not implemented yet. got stage , got stuck.
it's graph of adjacent list. has vertices , edges. got error cannot understand. can explain how error had come about? error
c:\users\jialee\documents\codeblocks\shortestpath\graph.cpp: in constructor 'edge::edge(vertex, weight)': c:\users\jialee\documents\codeblocks\shortestpath\graph.cpp:34:33: error: no matching function call 'vertex::vertex()'
and code
#include <forward_list> #include <string> using namespace std; const int max_size = 10000; typedef int weight; class vertex { public: vertex(string name, int num); string city_name; int city_num; }; class edge{ public: edge(vertex v, weight w); vertex associated_vertex; weight weight; }; class graph{ public: graph(int size); }; vertex::vertex(string name, int num){ city_name = name; city_num = num; } edge::edge(vertex v, weight cost){ associated_vertex = v; weight = cost; } graph::graph(int size = max_size){ forward_list<edge> g[size]; }
the error says missing default constructor (constructor no arguments) vertex, required during construction of edge.
basically, edge constructor tries first default initialize members , assign passed values.
you can either add default constructor vertex class or (better) use initializer lists in edge constructor:
edge::edge(vertex v, weight cost): associated_vertex{v}, weight{cost} { }
Comments
Post a Comment