无法实例化重载的模板化函数

时间:2017-07-26 19:36:02

标签: c++ templates overloading

我有一个名为Hashtable的类,有几种批量加载输入数据的方法。这些方法中的每一种都通过模板支持不同的文件格式,并且也被重载,因此可以使用字符串(文件名)或解析器对象作为其第一个参数来调用它。

这是一个例子。 consume_seqfile方法在类标题中定义,如此。

template<typename SeqIO>
void consume_seqfile(
    std::string const &filename,
    unsigned int &total_reads,
    unsigned long long &n_consumed
);

template<typename SeqIO>
void consume_seqfile(
    read_parsers::ReadParserPtr<SeqIO> &parser,
    unsigned int &total_reads,
    unsigned long long &n_consumed
);

然后在类定义文件的底部实例化,如此。

template void Hashtable::consume_seqfile<FastxReader>(
    std::string const &filename,
    unsigned int &total_reads,
    unsigned long long &n_consumed
);


template void Hashtable::consume_seqfile<FastxReader>(
    ReadParserPtr<FastxReader>& parser,
    unsigned int &total_reads,
    unsigned long long &n_consumed
);

这一切都很好,并且有好几个月。我现在正在尝试添加一个具有附加参数的此方法的新变体。它在标题中定义如此。

template<typename SeqIO>
void consume_seqfile_with_mask(
    std::string const &filename,
    Hashtable* mask,
    unsigned int &total_reads,
    unsigned long long &n_consumed
);

template<typename SeqIO>
void consume_seqfile_with_mask(
    read_parsers::ReadParserPtr<SeqIO>& parser,
    Hashtable* mask,
    unsigned int &total_reads,
    unsigned long long &n_consumed
);

并在源文件中实例化。

template void Hashtable::consume_seqfile_with_mask<FastxReader>(
    std::string const &filename,
    Hashtable* mask,
    unsigned int &total_reads,
    unsigned long long &n_consumed
);


template void Hashtable::consume_seqfile_with_mask<FastxReader>(
    ReadParserPtr<FastxReader>& parser,
    Hashtable* mask,
    unsigned int &total_reads,
    unsigned long long &n_consumed
);

但是,当我尝试编译时,我收到以下错误消息。

src/oxli/hashtable.cc:635:26: error: explicit instantiation of undefined function template 'consume_seqfile_with_mask'
template void Hashtable::consume_seqfile_with_mask<FastxReader>(
                         ^
include/oxli/hashtable.hh:281:10: note: explicit instantiation refers here
    void consume_seqfile_with_mask(

我的Google / StackOverflow技能让我失望。知道可能导致这个问题的原因吗?

更新:问题在于代码未显示。我做了有一个函数定义,但它缺少namespacing的正确Hashtable::前缀。所以...功能确实未定义。通过正确包含命名空间解决了问题。

1 个答案:

答案 0 :(得分:0)

您声明这些函数并在以后显式实例化它,但实际上您是否有函数的定义?

相关问题