0

I've been playing around with C++ templates (in an object-oriented style) and all is fine. I wondered if it was possible to use templates in a "functional way". I've included a snipped of dummy code (see below) and when trying to compile I get the following error:

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

I think the header file is incorrect, but I can't seem to find any examples of what I am trying to achieve (I'm not too sure if it's possible). If I ignore the .h and simply include the sort.cpp in main.cpp, then it complies and works.

Any help would be appreciated,

thanks.

//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;
}
clangers
  • 566
  • 1
  • 6
  • 13

0 Answers0