0

fyi I have looked at the other posts on this topic but I either didn't understand or didn't find my problem in them.

pretty much I am trying to put a class into a cpp and .h file, when I compile the code I get a warning saying "warning: inline function 'float map::get_mapped_value()' used but never defined.

And when I build the code I get an error saying undefined reference to 'map::get_mapped_value'

below are the code in each file

I thought I had all my includes correct, but maybe I am missing something, I am new to separating classes into seperate file, so any help would be greatly appreciated.

Thanks in advance, Dean.

map.cpp

#include "map.hpp"
#include <stdio.h>
#include <iostream>

using namespace std;

//------------------------Constructor definitions--------------------------
map::map(float Val_to_map_, float in_min_, float in_max_, float out_min_, float out_max_)
    : Val_to_map(Val_to_map_)
    , in_min(in_min_)
    , in_max(in_max_)
    , out_min(out_min_)
    , out_max(out_max_)
{
}
//-------------------------------------------------------------------------

//------------------------Copy Constructor definitions---------------------
map::map(const map& mp)
    : Val_to_map(mp.Val_to_map)
    , in_min(mp.in_min)
    , in_max(mp.in_max)
    , out_min(mp.out_min)
    , out_max(mp.out_max)
{
}
//-------------------------------------------------------------------------

//-----------------------equals function-------------------------------------
map& map::operator=(const map& mp)
{
    if(this == &mp)
        return (*this);

    Val_to_map = mp.Val_to_map;
    in_min = mp.in_min;
    in_max = mp.in_max;

    out_min = mp.out_min;
    out_max = mp.out_max;

    return (*this);
}
//---------------------------------------------------------------------------

//-------------------get function definitions--------------
inline float map::get_mapped_value()
{
    return (float((Val_to_map - in_min) * (out_max - out_min) / (in_max - in_min) + out_min));
}
//-----------------------------------------------------------

//-------------------set function definitions----------------
inline void map::set_Val_to_map(float _Val_to_map)
{
    Val_to_map = _Val_to_map; 
}

inline void map::set_in_min(float _in_min)
{
    in_min = _in_min;
}
inline void map::set_in_max(float _in_max)
{
    in_max = _in_max;
}
inline void map::set_out_min(float _out_min)
{
    out_min = _out_min;
}
inline void map::set_out_max(float _out_max)
{
    out_max = _out_max;
}

//-----------------------------------------------------------


map.h

#ifndef MAP_HPP
#define MAP_HPP

using namespace std;

class map
{
public:
    //----------------Special Member functions-----------------
    map(float Val_to_map_, float in_min_, float in_max_, float out_min_, float out_max_); // accepted arguments
    map(const map& m);                                                                    // copy constructor
    map& operator=(const map& m);                                                         // Equals function
    ~map()
    {
    } // destructor
    //---------------------------------------------------------

    //-----------------get functions-----------------
    inline float get_mapped_value();
    //------------------------------------------------

    //-----------------set functions-----------------
    inline void set_Val_to_map(float Val_to_map);
    inline void set_in_min(float in_min);
    inline void set_in_max(float in_max);

    inline void set_out_min(float out_min);
    inline void set_out_max(float out_max);
    //------------------------------------------------

private:
    float Val_to_map;
    float in_min; // min Vref
    float in_max; // max Vref

    float out_min; // min output voltage from psu
    float out_max; // max output voltage from psu
};

#endif // MAP_HPP

main.cpp

main(){

   map voltage(1, map_in_min, map_in_max, map_out_min, map_out_max);
   
   cout << "Mapped val: " << voltage.get_mapped_value() << endl;
}
Dean21
  • 13
  • 4
  • Does your `main.cpp` have `#include "map.hpp"`? – Igor Levicki Oct 11 '22 at 13:33
  • How do you build your code? You need to tell the compiler that `map.cpp` and `main.cpp` both need to be compiled and linked together. – NathanOliver Oct 11 '22 at 13:34
  • Take out all of those `inline` markers. Get the code to build, which should be straightforward. Then, if inlining is important, start moving function definitions from the .cpp file to the .hpp file and marking the moved functions `inline`. – Pete Becker Oct 11 '22 at 13:35
  • @Igor Levicki yes it does – Dean21 Oct 11 '22 at 13:38
  • See [Separating class code into a header and cpp file](https://stackoverflow.com/questions/9579930/separating-class-code-into-a-header-and-cpp-file) – Jason Oct 11 '22 at 13:38

1 Answers1

1

You declared the function get_mapped_value as an inline function

inline float get_mapped_value();

So its definition shall be in each compilation unit where it is used.

The simplest way to resolve the problem is to include the function definition either directly in the class definition where it is declared or in the header where the class definition is present.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335