Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Creator: “inline function used but never defined” – why?

Tags:

c++

qt

qt-creator

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.

like image 842
Tony the Pony Avatar asked Sep 14 '09 13:09

Tony the Pony


People also ask

Why would a compiler decide not inline a function?

Compiling with -Otime increases the likelihood that a function is inlined. Large functions are not normally inlined because this can adversely affect code density and performance.

When can an inline function not be used?

When we should avoid the use of inline? We should not use functions that are I/O bound as inline functions. When large code is used in some function, then we should avoid the inline. When recursion is used, inline function may not work properly.

What are the disadvantages of an inline function?

5) Inline functions may not be useful for many embedded systems. Because in embedded systems code size is more important than speed. 6) Inline functions might cause thrashing because inlining might increase size of the binary executable file. Thrashing in memory causes performance of computer to degrade.

What are the restriction of inline function?

Limitations of Inline FunctionsInline functions do not work if the body of the function contains any sort of looping or iteration. Inline functions do not support the use of switch or goto statements. C++ Inline functions cannot work if the function defined is recursive in nature.


3 Answers

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

like image 154
Johan Avatar answered Oct 14 '22 22:10

Johan


Inline methods are supposed to be implemented in the header file. The compiler needs to know the code to actually inline it.

Except if the inline function is used in the same project, possibly in another file that #include its header.

I miss there is such a restriction for libraries because restricting headers to function prototypes make things more readable.

What about #include-ing the .cpp ?

like image 20
Laurent Alebarde Avatar answered Oct 14 '22 21:10

Laurent Alebarde


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()
{ ... }
like image 36
147 Avatar answered Oct 14 '22 21:10

147