c++ - 对继承函数的 undefined reference

标签 c++ templates inheritance

我正在实现 A* 算法来解决几个问题 - 8 Puzzle 问题和另一个问题。对于 A Star,我在 A_star.hpp 中实现了三个泛型类:

template <class U>
class Heuristic{
public:
    virtual int getHeuristic(Node<U> currNode, Node<U> target);
};

template <class V>
class NextNodeGenerator{
public:
    virtual vector<pair<V, int> > generate(Node<V> curr);
};

template <class W>
class CompareVal{
public:
    virtual bool compare(W val1, W val2);
};

为了解决 8 Puzzle 问题,我在 prob2.cpp 中为上述每个泛型类实现了三个子类:

template <class hT>
class PuzzleHeuristic: public Heuristic<hT>{
public:
    virtual int getHeuristic(Node<hT> currNode, Node<hT> target){
        //Code for getHeuristic
    }
};

template <class cT>
class PuzzleCompareVal: public CompareVal<cT>{
public:
    virtual bool compare(cT val1, cT val2){
        //Code for compare
    }
};

template <class nT>
class PuzzleNNG: public NextNodeGenerator<nT>{
public:
    virtual vector<pair<nT, int> > generate(Node<nT> curr){
        //Code for generate
}

在 A_star.hpp 中,我还有一个 AStar 类:

template <class Y>
class AStar{
    Heuristic<Y> *h;
    NextNodeGenerator<Y> *nng;
    CompareVal<Y> *comp;

public:

    void setHeuristic(Heuristic<Y> *hParam){
        h = hParam;
    }

    void setNNG(NextNodeGenerator<Y> *nngParam){
        nng = nngParam;
    }

    void setCompareVal(CompareVal<Y> *compParam){
        comp = compParam;
    }

    vector<Node<Y> > solve(Y start, Y target){
        //Code for solve
    }

在 prob2.cpp 的 main() 函数中,我创建了一个 AStar 对象(Array 是我单独定义的模板类):

int main()
{
    PuzzleHeuristic<Array<int> > pH;
    PuzzleCompareVal<Array<int> > pCV;
    PuzzleNNG<Array<int> > pNNG;

    AStar<Array<int> > aStar;
    aStar.setHeuristic(&pH);
    aStar.setNNG(&pNNG);
    aStar.setCompareVal(&pCV);

    vector<Node<Array<int> > > answer = aStar.solve(start, target);
}

编译时出现以下错误:

/tmp/ccCLm8Gn.o:(.rodata._ZTV17NextNodeGeneratorI5ArrayIiEE[_ZTV17NextNodeGeneratorI5ArrayIiEE]+0x10): undefined reference to NextNodeGenerator<Array<int> >::generate(Node<Array<int> >)' /tmp/ccCLm8Gn.o:(.rodata._ZTV10CompareValI5ArrayIiEE[_ZTV10CompareValI5ArrayIiEE]+0x10): undefined reference toCompareVal >::compare(Array, Array)' /tmp/ccCLm8Gn.o:(.rodata._ZTV9HeuristicI5ArrayIiEE[_ZTV9HeuristicI5ArrayIiEE]+0x10): undefined reference to `Heuristic >::getHeuristic(Node >, Node >)' collect2: error: ld returned 1 exit status Blockquote

我怀疑问题是由于模板函数的继承造成的。什么可能导致错误?

最佳答案

所有虚函数都需要定义,即使它们在子类中被重写。如果您不想在基类中实现它们,并强制子类覆盖这些函数,您应该在基类中将它们设为抽象,例如

template <class V>
class NextNodeGenerator{
public:
    virtual vector<pair<V, int> > generate(Node<V> curr) = 0;
    //                                                  ^^^^
    //   This is what makes the function an abstract function 
};

关于c++ - 对继承函数的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36519534/

相关文章:

c++ - OpenCV 立体图像对校正...显示结果

c++ - 链接器错误:对 `Reference_Genome::seq[abi:cxx11]'的 undefined reference

c++ - 用 in_addr 结构中的点显示 IP 地址

javascript - 循环模板无法处理 ie11 中的数组

c++ - 如何通过自定义选择底层容器来制作树结构?

java - 如何加入 Hibernate/JPA 2.0 CriteriaQuery 中的 JOINED 子类?

mysql - 单表继承和字段列表中的未知列

c++ - 将子对象复制到父对象

c# - 通用类型层次结构的 DataContractSerialization

c++ - 如何为编译时已知的参数的多个值编译函数