-3
class AdjacencyList : public Graph{
    private:
        std::vector<Edge> edges;
    public:
        AdjacencyList();
        void add_edge(const Edge& e){
            edges.push_back(e);

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

The issue I'm getting is that it gives me the following error:

/tmp/ccqBzTGI.o: In function `main':
test.cpp:(.text+0x3d): undefined reference to `AdjacencyList::AdjacencyList()'
/tmp/ccqBzTGI.o: In function `Graph::Graph()':
test.cpp:(.text._ZN5GraphC2Ev[_ZN5GraphC5Ev]+0xf): undefined reference to `vtable for Graph'
/tmp/ccqBzTGI.o:(.rodata._ZTI15AdjacencyMatrix[typeinfo for AdjacencyMatrix]+0x10): undefined reference to `typeinfo for Graph'
/tmp/ccqBzTGI.o:(.rodata._ZTI13AdjacencyList[typeinfo for AdjacencyList]+0x10): undefined reference to `typeinfo for Graph'
collect2: ld returned 1 exit status

I'm not sure how to even approach this issue.

Edit: I'm a fool and forgot to define a constructor I wrote.

Timothy
  • 3
  • 5

2 Answers2

1

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.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
1
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.