# Linker errors when function is moved

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

## Recommended Posts

##### Share on other sites
Are Timer::Timer/Tile::get_type defined in a header file? If so can you move them into a source file OR make them inline?

Again, are fpstimer/metanim defined in a header file? If so can you move them into a source file?

##### Share on other sites
Quote:
 Original post by HodgmanIs Timer::Timer defined in a header file? If so can you move it into a source file OR make it inline?

Yes, its defined in a header file, I'll try to just move it to each of the source files to see if it works.
And what is "making it inline"?
EDIT : I'll have to find out what that inline thing is. I can't move it, since there's classes in other header files dependent on this one. It seems that every single header file I have is giving errors, though..
EDIT2: Also, I moved metanim to the menus source file, and its still giving the same error.

##### Share on other sites
#includeing a header is pretty much the same as copy/pasting it into your source files - so if you've defined a function or variable in a header, then when you include that header in more than one source file, then each source is going to have it's own copy of it!

There is a special keyword - inline - which tells the compiler "this function is so small that you can pretend that it's not really a function, if you like". Usually this is used for optimisation, (as it basically allows the compiler to just copy&paste the body of the function into the place where you call it from instead of actually performing a function call) but it also has the side effect of allowing you to tell the linker to get over the fact that the function is going to be defined in multiple source files.

[EDIT]
Here's an example header with 3 classes, Foo, Bar and Baz.
#ifndef STUFF#define STUFFclass Foo{public:  Foo()  {    DoStuff();  }};class Bar{public:  Bar();};inline Bar::Bar(){  DoStuff();}class Baz{public:  Baz();};Baz::Baz(){  DoStuff();}#endif

Foo is ok because by writing functions inside the class definition like that, the compiler automatically makes them inline.

Bar is ok because we've specifically told that function to be inline.

Baz is *not* ok, because the function is not inline and it is defined in the header - so if you include (which == copy&paste, remember) this header into more than one source file, the linker will end up complaining about Baz::Baz being defined multiple times.
The solution to this is just to put all function definitions into source files (inlining is not really recommended).

Solving your linker problems for global variables is a little different - you need to use the extern keyword in the header to say "this variable exists, but i'm not defining it", and then define it as usual in one of the source files.

So to solve this:
//in the header:class Baz{public:  Baz();};extern Baz myVariable;//"a Baz exists somewhere called myVariable"//in *one* of the CPP files:#include "baz.h"Baz myVariable;//The actual variable lives in this source fileBaz::Baz(){  DoStuff();}