c++ - 模板类调用模板函数 - 链接器错误

标签 c++ templates linker-errors

我正在试验模板类和函数,但遇到了以下错误。我正在尝试使用 RelationShipExpander 类中的模板类调用 Vertex 类中的模板函数。下面的 MWE 会产生以下链接器错误:

Undefined symbols for architecture x86_64:
  "typeinfo for PipeType", referenced from:
      typeinfo for RelationShipExpander<in> in main.cpp.o
  "vtable for PipeType", referenced from:
      PipeType::PipeType() in main.cpp.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
#include <iostream>
#include <vector>
#include <forward_list>

using namespace std;

// Abbrr
class Vertex;
typedef forward_list<const Vertex*> edge_list;

/*
 * Matching
 */
struct match_first {
    match_first(unsigned value) : value(value) {};

    template<class A>
    bool operator()(const pair<const unsigned, A> &a) {
        return a.first == value;
    }

    unsigned value;
};

/*
 * Edge Indirection
 */
struct in {
    static edge_list& get_list(pair<edge_list, edge_list> &a) {
        return a.first;
    }
};

struct out {
    static edge_list& get_list(pair<edge_list, edge_list> &a) {
        return a.second;
    }
};


class Vertex {
public:
    template<class Direction>
    edge_list &get_edges(unsigned relation_id) {
        auto i = find_if(neigh_set.begin(), neigh_set.end(), match_first(relation_id));

        if (i != neigh_set.end()) {
            return Direction::get_list(i->second);
        }

        // throw except.
    }

private:
    vector<pair<const unsigned, pair<edge_list, edge_list>>> neigh_set;
};



class PipeType {
public:
    /*
     * Mutators
     */
    virtual void Pipe(Vertex& gremlin);
};



template <class Direction>
class RelationShipExpander: PipeType {
public:
    /*
     *  Constructor
     */
    RelationShipExpander(unsigned relationship_id) : relationship_id(relationship_id) {};

    /*
     *  Mutators
     */
    void Pipe(Vertex& gremlin) {
        gremlin.get_edges<Direction>(relationship_id);
    };
private:
    unsigned relationship_id;
};



int main() {
    RelationShipExpander<in> expand_in(1);
}

最佳答案

PipeType 不同的地方在于成员函数好像没有定义。就像编译器说的那样:

"vtable for PipeType", referenced from:
PipeType::PipeType() in main.cpp.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.

如果您打算将 PipeType 作为一个接口(interface),您可以通过添加 = 0 而不是函数体来使该函数成为纯虚拟函数。你不能直接跳过实现,即使它是在派生类中实现的。

class PipeType {
public:
    virtual void Pipe(Vertex& gremlin) = 0;
};

如果您想让类实现该功能,您当然必须在某处添加该实现。在 header 中或在其中一个 .cpp 文件中。

关于c++ - 模板类调用模板函数 - 链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43074034/

相关文章:

c++ - 字符数组顺序错误

c++ - 编译器错误C2541- 'delete' : delete : cannot delete objects that are not pointers

c++ - 单例类 - 哪个方法

python - Django:奇怪的 TemplateDoesNotExist 错误

python - Mako:将变量传递给基本模板和子模板

C++ 在类型列表中创建模板特化

c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?

linux - 错误 : undefined reference to NetCDF functions

c++ - 如何初始化 parent 的成员?

c++ - 如何在windows中使用柯南的SDL2