0

I made this function to convert a vector into a valarray:

template <typename T> valarray<T>* ValArrayTools::vectorToValArray(vector<T>* vector) {
valarray<T>* val = new valarray<T>(vector->size());
for(int i = 0; i < vector->size(); i++)
{
    val[i] = vector[i];
}
return val;

}

I tried to use a template so that I can use this function with every possible type.

Now I want to convert a

vector<short int> buffer;

into a

valarray<short int>* data;

by using

sf->data = ValArrayTools::vectorToValArray<short int>(&buffer);

But I get this error:

CMakeFiles\LPC-Vocoder_Tests.dir/objects.a(ShortFileLoader.cpp.obj): In function `ZN15ShortFileLoader17generateShortFileEv':
C:/Users/****/Documents/Uni/SS18/**Projekt/LPC-Vocoder/src/ShortFileLoader.cpp:33: undefined reference to `std::valarray<short>* ValArrayTools::vectorToValArray<short>(std::vector<short, std::allocator<short> >*)'
melpomene
  • 84,125
  • 8
  • 85
  • 148
leet
  • 499
  • 1
  • 4
  • 11
  • 1
    Implement the method inside the header. Not in the `cpp`. – Hatted Rooster Apr 19 '18 at 16:46
  • Looks like you might have your template split in a .h and .cpp file, you can't do that. Also there is not reason to use pointers here at all. You function could be written as: http://coliru.stacked-crooked.com/a/db3eb466a1a069e7 – NathanOliver Apr 19 '18 at 16:48
  • When the template is called, if the definition is not visible to the compiler, it'll assume that someone else will instantiate the function and generate a link to it. But nobody else instantiates it, so you get the link error. Moving the def to the header will make it visible and allow it to be instantiated. – Chris Uzdavinis Apr 19 '18 at 16:48
  • I moved the code into the header and now it compiles. But now i get a segmentation fault and my debugger still says that its just a std::valarray* – leet Apr 19 '18 at 17:03
  • short == short int – leet Apr 19 '18 at 17:23
  • I've rolled back your edit because it substantially changes the question (the comments and the duplicate no longer apply). You're better off posting a completely new question with a [mcve]. – melpomene Apr 19 '18 at 17:52

0 Answers0