# Another include problem (inline functions)

This topic is 3944 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

To solve a mutual include puzzle, I had to put the declaration of a function in a .h file and the definition in a .cpp file:
//in A.h
void myMethod();

//in A.cpp
void myMethod() {
doSomething();
}


This works, but when I try to add inline, I get "unresolved external" problems! I read somewhere (http://csg.lbl.gov/pipermail/vxwexplo/2003-March/000711.html) that inline methods must be defined in the .h file to avoid this, but I can't have the definition in the .h file due to the mutual include puzzle that I described in a previous thread here. I've tried adding the inline keyword to 1. the definition, 2. the declaration, 3. both, but nothing works. Note, the error does NOT appear when simply compiling A.h and A.cpp, but it appears when I try to use the method like this:
//in main.cpp
#include "A.h"
int main() {
myMethod();
}


...after trying to make the method inline in any of the 3 ways mentioned above. It works fine when it's not inline, though, but I will have some 20 one-line methods like this so I really don't want them all to be non-inline. How do I solve this? Or do I just have to accept that C++ by design in some cases won't allow inline methods when you put the classes in different files? I would avoid the mutual include problems if I put everything in a single huge .h and .cpp file, but that isn't very nice because the .h file would then be >5,000 line of code...

##### Share on other sites
Yes, C++ by design requires inline functions to be in the header so that every file that needs them can see them. (Actually, I don't think this is required by the standard, but it is required by almost every implementation.) Each .cpp file is compiled separately, so they can't inline a function that only exists in another .cpp file - they just don't have that information available.

Mutual inclusion issues tend not to be related to inline functions, because inline functions are supposed to be very simple and not require significant dependencies. Could you post a small example of such functions?

##### Share on other sites
There is a way to solve it - it requires creating extra files. Lets say you have foo.h and bar.h dependant on each other due to inline functions. What you do is create foo.inl and bar.inl files. Move the inline functions to those, declared as if they would be in the cpp, but all tagged as inline. For example:

inline bar *foo::MakeBar(void){  return new bar();}

You then rename foo.h and bar.h to foo_h.h and bar_h.h. Create a new foo.h and bar.h that both contain:

#include "foo_h.h"#include "bar_h.h"#include "foo.inl"#include "bar.inl"

It should now compile and link correctly.

##### Share on other sites
You don't need extra files to have inline functions in headers with circular inclusion. You just need to finesse the standard header file format a bit. Ex:
A.h:
class A {  // stuff};#include "B.h"void A::some_function_using_B(void) {  // stuff}

B.h:
class B {  // stuff};#include "A.h"void B::some_function_using_A(void) {  // stuff}

##### Share on other sites
Thanks for all replies! I think I will be able to solve it now.

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5
JoeJ
10

• 20
• 13
• 14
• 76
• 22
• ### Forum Statistics

• Total Topics
632139
• Total Posts
3004369

×