0
/*Graph.h*/
template<typename Tv, typename Te>
class Graph
{
public:
    virtual ~Graph() = 0;

    /*顶点操作*/
    ...

    /*边操作*/
    ...

protected:
    int n;  //顶点数
    int e;  //边数
};
#pragma once
#include "Graph.h"
#include <vector>
#include <iostream>

template<typename Tv> struct Vertex
{
    ...
};

template<typename Te> struct Edge
{
    ...
};


template<typename Tv, typename Te>
class GraphMatrix : public Graph<Tv, Te>
{
public:
...
    ~GraphMatrix() {
        for (int i = 0; i != n; ++i)
        {
            for (int j = 0; j != n; ++j)
            {
                delete E[i][j];
            }
        }
    }
...
};

when i Comment out virtual ~Graph() = 0; in Graph.h, I can run the program again. The complier said 错误 LNK2019 无法解析的外部符号 "public: virtual __thiscall Graph<char,int>::~Graph<char,int>(void)" (??1?$Graph@DH@@UAE@XZ),该符号在函数 "public: virtual __thiscall

Neo
  • 1
  • 1
    It's fine to have a virtual destructor, but you still need an implementation. The `= 0` makes it a pure virtual destructor which doesn't work. Virtual dispatch works differently during constructors and destructors because they're run when the otherwise "most derived object" is in an invalid state. – Nathan Pierson Feb 20 '21 at 03:59
  • Making a base class *indestructible* (because there is no implementation) is a bit of an impediment. – Eljay Feb 20 '21 at 04:31
  • @JamiT Oh, thanks for your reminder. I thinking this is what i want. – Neo Feb 20 '21 at 05:49
  • 1
    Please note for your future questions that Stack Overflow is for English content only. – costaparas Feb 20 '21 at 06:13

0 Answers0