A little backstory: I was working on a MaxHeap class that implements heapsort as a method. It uses primitive integer arrays. I was wanting to test this algorithm with random inputs against the heapsort algorithm included in the <algorithms> library. I discovered however that I could only use vectors with that algorithm and so I wanted to code a function that would allow me to allocate space for and copy a vector<int> into an integer array.
This is what I wrote:
using namespace std;
int *vecToAry(vector<int> input)
{
int *output = new int[input.size()];
copy(input.begin(), input.end(), output);
return output;
}
After putting lots of utility functions together into said library I wanted to make some of them generic. So the above code then became:
using namespace std;
template <typename T>
T *vecToAry(vector<T> input)
{
T *output = new T[input.size()];
copy(input.begin(), input.end(), output);
return output;
}
However, when I compile with the following driver I receive the linker errors below.
#include <iostream>
#include <vector>
#include "cgUtil.h"
using namespace std;
int main()
{
int a[5] = {1,2,3,4,5};
vector<int> v(a, a+5);
int *result = vecToAry(v);
ENTER_EXIT(0);
}
Error 1 error LNK2019: unresolved external symbol "int * __cdecl vecToAry<int>(class std::vector<int,class std::allocator<int> >)" (??$vecToAry@H@@YAPAHV?$vector@HV?$allocator@H@std@@@std@@@Z) referenced in function _main C:\Users\cory\Desktop\Dropbox\Dev\Source\C++\HeapSort\HeapSort\heapsort.obj
I am not sure what I am doing wrong, or how I am supposed to create a generic function to do something similar if this is not possible. Anyone care to share some insight?

New Topic/Question
Reply




MultiQuote









|