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"; } ... 
4
  • 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. Commented Sep 29, 2014 at 17:29
  • See here - stackoverflow.com/questions/5057021/… Commented Sep 29, 2014 at 17:30
  • This is not duplicated because I have added a definition in the header. Please read my question before marking duplicated. Commented Sep 29, 2014 at 17:43
  • that's a declaration, not a definition. I did not dup this, but it's pretty well-trodden ground. See stackoverflow.com/questions/1410563/… Commented Sep 29, 2014 at 19:29

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
@NeilKirk Then you will have a different declaration and definition of the class.
@NeilKirk Because you are defining a function that was not declared. It would still compile and work, but just semantically they are different.
@Zan but I have added an inlined add method in my header file though?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.