C ++如何使用向量与模板? [英] C++ How to use vectors with templates?

查看:236
本文介绍了C ++如何使用向量与模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过一个算法文本,试图实现C ++中的一切实践。但我似乎不知道模板。我有三个文件:
algPlayground.h

I'm working through an algorithm text, trying to implement everything in C++ for practice. But I can't seem to figure out templating.I have three files: algPlayground.h

#include <stdlib.h>
#include <vector>

using namespace std;

template <class T> void insertionSort(vector<T>& toSort); 

algPlayground.cpp

algPlayground.cpp

#include <stdlib.h>
#include <vector>
#include "algPlayground.h"

using namespace std;

template <class T> void insertionSort(vector<T>& toSort) {
    for (int j=1; j < toSort.size(); ++j) {
        T key = toSort[j];
        int i = j-1;

        while (i > -1  && toSort[i] > key) {
            toSort[i+1] = toSort[i];
            i -= 1;
        } // end while

    toSort[i+1] = key;

    } // end for

} // end insertionSort

和algTest.cpp

and algTest.cpp

#include <stdlib.h>
#include <vector>
#include <iostream>
#include "algPlayground.h"

using namespace std;

int main() {

    vector<int> vectorPrime(5);

    vectorPrime[0]=5;
    vectorPrime[1]=3;
    vectorPrime[2]=17;
    vectorPrime[3]=8;
    vectorPrime[4]=-3;

    insertionSort(vectorPrime);
    for (int i=0; i<vectorPrime.size(); ++i) {
        cout << vectorPrime[i] << " ";
    }// end for
}

我得到以下错误: / p>

I get the following error:

algTest.cpp:(.text+0xb1): undefined reference to `void insertionSort<int>(std::vector<int, std::allocator<int> >&)'
collect2: error: ld returned 1 exit status


b $ b

我看到了此主题其中有人建议正确的方式是

I saw this thread where, someone suggested that the proper way to do this was

template<typename T, typename A>
void some_func( std::vector<T,A> const& vec ) {
}

但是当我进行更正时,我仍然得到类似的错误:

but when I make that correction, I'm still getting a similar error:

algTest.cpp:(.text+0xb1): undefined reference to `void insertionSort<int, std::allocator<int> >(std::vector<int, std::allocator<int> >&)'
collect2: error: ld returned 1 exit status

我不知道我在哪里出错。帮助?

I have no idea where I'm going wrong here. Help?

推荐答案

您的问题是,您需要在头文件中实现模板。编译器需要能够看到模板的实现,只要它被实例化,以便它可以生成适当的代码。所以只需将定义从 algPlayground.cpp 移动到 algPlayground.h

Your problem is that you need to implement the template in the header file. The compiler needs to be able to see the implementation of the template whenever it is being instantiated so that it can generate the appropriate code. So just move the definition from algPlayground.cpp to algPlayground.h.

另一种实现同样的方法是逆向 #include s,这样在 algPlayground.h #includealgPlayground.cpp。喜欢这种方法的人通常使用 tpp 扩展来实现文件,以清楚发生了什么。

An alternative approach that achieves the same thing is to reverse the #includes, so that at the bottom of algPlayground.h you #include "algPlayground.cpp". People who like this approach often use a tpp extension for the implementation file to make it clear what's going on.

这篇关于C ++如何使用向量与模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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