c++ inline function problem

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

Recommended Posts

Ok,I was messing aroung with some code and a problem appeared:

FILE1.CPP

#include <iostream>
using namespace std;
inline void fishy(){
cout << "da";
}

main.cpp

#include <iostream>
#include <string>

using namespace std;

extern void fishy();

int main() {
fishy();

}


As you can see I have 2 files: 1 contains an inline function definition,and the other declares the function as extern.When I try to compile the code i get a linker error.

I just need someone to tell me if I'm right but: inline functions don't have external linkage? And if that is true,what is the alternative? Using header files for all of them?

Share on other sites

Yep, inline means no external linkage.

Put the definition in a header included by the cpp files that use the function (or put them in a cpp file which you #include in a header, or call it myfile.inl instead if you want a different extension).

Share on other sites

Regardless of whether or not the compiler actually inlines the function, it still treats it semantically as though it were, so there is no function call to link against.

Share on other sites

Ok,no external linkage for the poor inline functions:) Thanks

Share on other sites

Make sure you don't put using namespace std in a header file though, that defeats the purpose of namespaces. You need to fully qualify std::cout or put using std::cout inside the function body to minimise any namespace symbol imports.

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
13

• 14
• 10
• 25
• 9
• 57
• Forum Statistics

• Total Topics
632640
• Total Posts
3007618
• Who's Online (See full list)

There are no registered users currently online

×