Question

I'm trying to force inline a member function and I get the error:

"a_class::mem_func()", referenced from:
func(a_class&)    in func.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

here is the distilled error-producing code

a_class.h
    #ifndef A_CLASS_H  #define A_CLASS_H
    class a_class {public: __inline__ void mem_func(); };
    #endif

a_class.cpp
    #include "a_class.h"
    __inline__ void a_class::mem_func() {}

func.h
    #ifndef FUNC_H  #define FUNC_H  #include"a_class.h"
    void func(a_class & obj);
    #endif

func.cpp
    #include "func.h"
    void func(a_class & obj) {obj.mem_func();}

main.cpp
    #include <iostream>  #include "func.h"  #include "a_class.h"
    int main () {a_class obj; func(obj);}

I'm using Xcode / gcc

Was it helpful?

Solution

If you want to make sure your function is inlined, move the definition in the header:

a_class.h
    #ifndef A_CLASS_H  #define A_CLASS_H
    class a_class {public: __inline__ void mem_func() {} /*<-definition*/; };
    #endif
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top