Error regarding polymorphism and const


Keywords:c++ 


Question: 


2 Answers: 

class AdjacencyList : public Graph{
private:
    std::vector<Edge> edges;
public:
    AdjacencyList();
    void add_edge(const Edge& e){
        int i,j;
        edges.push_back(e);

    }
    void print(){
        for(int i = 0;i<edges.size() ; i++){
            std::cout << "(" << edges[i]  << ")";
        }
    }

You have declared the default constructor AdjacencyList(), but haven't given it a body definition.



Look for the missing constructor, and missing member functions, just like the error messages say. In this case, the error messages may not be 100% clear, but, trust me, compared to typical C++ error messages, these are better than average, by far, in terms of clarity. Always attempt to figure out and understand the compiler's error message. With time and practice, it shouldn't take long before you are able to make sense of most of them.

You declared a

AdjacencyList();

constructor. You apparently did not define it anywhere.

You declared an entire class:

class Graph{
    public:
        virtual void add_edge(const Edge& e);
        virtual void print();
        friend std::ostream& operator<<(std::ostream& os, const Edge& obj);
};

And you defined none of its methods, apparently. If this is supposed to be an abstract interface class, these methods should be defined as pure virtual methods.