• Advertisement
Sign in to follow this  

c++ inline function problem

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
Share on other sites

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement