与非模板类的模板构造函数的麻烦 [英] Trouble with template constructor of non-template class

查看:171
本文介绍了与非模板类的模板构造函数的麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在搜索其他问题的答案时开发了一个助手类,可以使用任何种类的整数容器。具体来说,我的类将根据一些标准来查找容器值。为了使用不同类型的容器,我的类显然需要不操作容器本身,而是他们的迭代器。我不需要在我的类中的任何地方引用容器,我只使用迭代器,只在类构造函数。

While searching for an answer to another question I developed a a helper class that would work with any kind of container of integers. Specifically, my class would lookup container values based on some criteria. In order to work with different types of containers, my class obviously needs to operate not on containers themselves but on their iterators. I don't need to reference containers anywhere in my class, I use only iterators and only in class constructor.

//头文件。

 class MyLookup {
    public: 
    template<typename ForwardIt>   // Forward iterator though sequence of integers
    MyLookup(ForwardIt begin, ForwardIt end)
    ...
 }

//实施文件

 template<typename ForwardIt>
 MyLookup::MyLookup(ForwardIt begin, ForwardIt end) {
 ...
 }

//使用文件

std::vector<int> foo;
...
MyLookup lookup(foo.begin(), foo.end());

当使用CLang时,初始编译器通过成功,但是我得到链接器错误:

While using CLang the initial compiler pass succeeds but then I get linker errors:

架构x86_64的未定义符号:
MyLookup :: MyLookup>(std :: __ 1 :: __ wrap_iter,std :: __ 1 :: __ wrap_iter),引用自:...

Undefined symbols for architecture x86_64: "MyLookup::MyLookup >(std::__1::__wrap_iter, std::__1::__wrap_iter)", referenced from: ...

任何想法我做错了什么?

Any idea what am I doing wrong?

推荐答案

为头文件中的模板提供定义,而不是自己的源文件。这是因为模板字面上是类定义的模板 - 它们本身不是类定义。

You need to provide the definition for your templates in header files, not their own source files. This is because templates are literally templates for class definitions -- they are not class definitions themselves.

这篇关于与非模板类的模板构造函数的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆