views:

1262

answers:

2

Why am I getting this warning in Qt Creator: ` inline function ‘bool Lion::growl ()’ used but never defined?

I double-checked my code, and have a declaration

inline bool growl () in Lion (lion.h)

and the corresponding implementation in lion.cpp:

inline bool Lion::growl ()

What’s going on?

EDIT: My assumption has been that it is legal to define the actual inline method in the .cpp file (the inline keyword alerts the compiler to look for the method body elsewhere), or am I mistaken?

I don't want to clutter my header files with implementation details.

+5  A: 

Well, I don't know the exact problem, but for starters:

  • Inline methods are supposed to be implemented in the header file. The compiler needs to know the code to actually inline it.
  • Also using the "inline" keyword in the class declaration doesn't have any effect. But it cannot hurt either.

See also: c++ faq lite

Johan
Personally (and this is a matter of taste), I *hate* code that explicitly marks code as inline - either with the inline keyword, or by putting the implementation in the header file for 2 reasons: 1) it breaks encapsulation. 2) it hurts performance - yes, you heard me. Who do you think knows whether inlining a mathod will result in a performance boost best: the compiler, or the programmer. Seriously people, trust your optimisers, they do a better job (in many cases, but not all) then you ever will.
Thomi
A: 

In addition to what Johan said, you cannot have a separate definition and declaration for the function even if both are in the same header file. This holds true especially for member functions of classes. The function code should be of the form:

class someClass
{
void someFunc()
{ ... }
}
// This will make the function inline even w/o the explicit 'inline'

And NOT of the form

class someClass
{
public:
     void someFunc();
}

void someClass::someFunc()
{ ... }
Aashay