0

I want to have a static function computing the mean of a vector of floats. I take the float function from here.

I have a simplemath.h

#ifndef SIMPLEMATH_H
#define SIMPLEMATH_H

class SimpleMath
{
public:
    SimpleMath();

    static float
    get_median(std::vector<float> floats);
};

#endif // SIMPLEMATH_H

and a simplemath.cpp:

#include "simplemath.h"

SimpleMath::SimpleMath()
{

}

float SimpleMath::get_median(std::vector<float> floats)
{
   float median;
   size_t size = floats.size();

   sort(floats.begin(), floats.end());

   if (size  % 2 == 0)
   {
       median = (floats[size / 2 - 1] + floats[size / 2]) / 2;
   }
   else
   {
       median = floats[size / 2];
   }

   return median;
}

and my main.cpp:

#include "simplemath.h"
int
main (int argc, char** argv)
{


   std::vector<float> vec;
   vec.push_back(1);
   vec.push_back(2);
   vec.push_back(5);
   vec.push_back(3);
   vec.push_back(4);

   float med =  SimpleMath::get_median(vec);
   std::cout << med;
}

I get the following linker error, unfortunately in French, which I cannot change atm:

main.obj:-1: error: LNK2019: symbole externe non résolu "public: static float __cdecl SimpleMath::get_median(class std::vector<float,class std::allocator<float> >)" (?get_median@SimpleMath@@SAMV?$vector@MV?$allocator@M@std@@@std@@@Z) référencé dans la fonction main
Community
  • 1
  • 1
Jan Hackenberg
  • 481
  • 3
  • 14

0 Answers0