0

我一直在玩 C++ 模板(以面向对象的风格),一切都很好。我想知道是否可以以“功能方式”使用模板。我已经包含了一段虚拟代码(见下文),并且在尝试编译时出现以下错误:

undefined reference to `std::vector<int, std::allocator<int> > bubble_sort<int (std::vector<int, std::allocator<int> >)'

我认为头文件不正确,但我似乎找不到任何我想要实现的示例(我不太确定是否可能)。如果我忽略 .h 并简单地将 sort.cpp 包含在 main.cpp 中,那么它就可以正常工作。

任何帮助,将不胜感激,

谢谢。

//sort.h
#include <vector>

template<class T> 
std::vector<T> bubble_sort(std::vector<T> list);


//sort.cpp
#include <vector>
#include "sort.h"
using namespace std;

template <class T> vector<T> bubble_sort(vector<T> list)
{
  return list;
}


//main.cpp
#include <vector>
#include "sort.h"
using namespace std;

int main()
{
  vector<int> list;

  list.push_back(10);
  list.push_back(5);
  list.push_back(6);

  bubble_sort(list);

  return 0;
}
4

0 回答 0