c++ - gtest problem with inline function -


hello have include inline function, when try testing class google test, have error like:

 error lnk2019: unresolved external symbol "public: double __thiscall math::returnpi(void)" (?returnpi@math@@qaenxz) referenced in function "private: virtual void __thiscall speed_math_test::testbody(void)" (?testbody@speed_math_test@@eaexxz) 

for example class(header file)

class math { public:     math(void);     inline double returnpi();     ~math(void); }; 

my class(cpp file)

math::math(void) {} math::~math(void) {} double math::returnpi() { return 3.14;} 

test:

test(eq, math) {     math *m=new math();     expect_eq(3.14,m->returnpi()); } 

what need do? read manual dont see how can resolved error.

an inline function should in header file, not in source file can inlined callers (which don't have access source file).

moreover, don't need specify inline in class declaration if give definition of function.

so header should become:

class math { public:     math(void);     double returnpi() { return 3.14; } // no need specify inline here     ~math(void); }; 

and remove definition returnpi() source file.

note have done:

class math { public:     math(void);     double returnpi();     ~math(void); };   inline double math::returnpi() { return 3.14; } // inline mandatory here avoid respecting "one definition rule" 

the second solution if want keep class declaration separate function definition.

also note inline not guarantees actual function calls inlined: thing enforces don't have respect "one definition rule": inline function must have same definition in translation units.


Comments