0

I am learning c++ by writing commons data structures and I have a compiler warning that my inlined add method is not defined.

src/vector.h:10:14: warning: inline function 'Vector::add' is not defined
      [-Wundefined-inline]
        inline void add(const float val);

What am I doing wrong? As far as I can tell the methods match up. However if I remove the inline method, it works fine but the first invocation of add takes 11380us but the 2nd and 3rd are around 3667us -nearly 4x penalty cost.

src/vector.h

//#include <cstddef>

class Vector {
public:
    explicit Vector(const int n);
    explicit Vector(const int n, const float val);
    float& operator[](const int i);
    inline int const length();
    inline void fill(const float val);
    inline void add(const float val);
    inline float sum();
private:
    float* arr;
    int len;
};

src.vector.cpp

#include "vector.h"
#include <iostream>
#include <algorithm>
#include "yepCore.h"
#include "yepMath.h"
#include "yepLibrary.h"
#include <cstdlib>

using namespace std;

inline void Vector::add(const float val)
{
    chrono::steady_clock::time_point start = chrono::steady_clock::now();   
    for (int i = 0; i < len; ++i) {
        arr[i] += val;
    }
    chrono::steady_clock::time_point end = chrono::steady_clock::now();
    cout << "yepp add took " << chrono::duration_cast<chrono::microseconds>(end - start).count()
              << "us.\n";
}

/**
template <>
void Vector<float>::add(const float val)
{
    chrono::steady_clock::time_point start = chrono::steady_clock::now();   
    yepCore_Add_V32fS32f_V32f(arr, val, arr, len);
    chrono::steady_clock::time_point end = chrono::steady_clock::now();
    cout << "yepp add took " << chrono::duration_cast<chrono::microseconds>(end - start).count()
              << "us.\n";

}
...
user2864740
  • 60,010
  • 15
  • 145
  • 220
jimjampez
  • 1,766
  • 4
  • 18
  • 29
  • I should point out that it was templated but removed it to simplify and debug as I thought it was because of the templating but the problem still exists. – jimjampez Sep 29 '14 at 17:29
  • See here - http://stackoverflow.com/questions/5057021/why-are-c-inline-functions-in-the-header – Steve Townsend Sep 29 '14 at 17:30
  • This is not duplicated because I have added a definition in the header. Please read my question before marking duplicated. – jimjampez Sep 29 '14 at 17:43
  • that's a declaration, not a definition. I did not dup this, but it's pretty well-trodden ground. See http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration – Steve Townsend Sep 29 '14 at 19:29

1 Answers1

5

Inline functions must be defined in the header. If they are not defined in the header file, then the function cannot be inlined, because callers won't have the definition.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • 2
    They can be defined in a source file if they are only used in the source file. And functions not in a header can still be inlined. – Neil Kirk Sep 29 '14 at 17:32
  • @NeilKirk Then you will have a different declaration and definition of the class. – SwiftMango Sep 29 '14 at 17:33
  • @NeilKirk Because you are defining a function that was not declared. It would still compile and work, but just semantically they are different. – SwiftMango Sep 29 '14 at 17:36
  • @Zan but I have added an inlined add method in my header file though? – jimjampez Sep 29 '14 at 17:39