-1

I want implement a function in c++ which returns the minimum of an array of any type. My code for now looks like this:

main.cpp:

#include <iostream>
#include <bits/unique_ptr.h>
#include "MathHelper.h"

using namespace std;

int main() {
    std::array<int, 3> myarray = {10,20,30};
    int i = MathHelper::minimum(myarray);
    printf("%d\n", i);
    return 0;
}

MathHelper.h:

#ifndef OPTIMIERUNG_MATHHELPER_H
#define OPTIMIERUNG_MATHHELPER_H


#include <stddef.h>
#include <array>

class MathHelper {
public:
    template <typename T, size_t SIZE>
     static T minimum(std::array<T, SIZE>& a);

};


#endif //OPTIMIERUNG_MATHHELPER_H

MathHelper.cpp:

#include "MathHelper.h"

template<typename T, size_t SIZE>
T MathHelper::minimum(std::array<T, SIZE> &a) {
    int minimum = a[0];
    if(SIZE>0){
        for(size_t i=1;i<SIZE;i++){
            if(a[i] < minimum){
                minimum = a[i];
            }
        }
    }
    return minimum;
}

executing this program results into following exception:

undefined reference to `int MathHelper::minimum<int, 3ul>(std::array<int, 3ul>&)'
Cœur
  • 37,241
  • 25
  • 195
  • 267
Finkes
  • 496
  • 5
  • 16

1 Answers1

0

As rozina and Joachim Pileborg commented template functions have to be implemented inside the header.

Finkes
  • 496
  • 5
  • 16