Jump to content
  • Advertisement
Sign in to follow this  
Mizipzor

Function definitions and LNK2005

This topic is 3685 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

In my corelib I have some functions definitions. SomeFile.h
#pragma once

int foo( int x )
{
   return x * 2;
}
However, when linking the core into my current project, the above function yields a LNK2005. The solution is to split it into two files: SomeFile.h
#pragma once

int foo( int x );
SomeFile.cpp
#pragma once
#include "SomeFile.h"

int foo( int x )
{
   return x * 2;
}
But its cumbersome to do this. What must I do to be able to both define and decleare a function in the header file?

Share this post


Link to post
Share on other sites
Advertisement
It may seem cumbersome to do this for a single function, but if that header file has the implementations of 10s of functions in it, it becomes more cumbersome than splitting the declaration and implementation up. With the implementation in the header it becomes harder to determine what functions the header contains.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mizipzor
What must I do to be able to both define and decleare a function in the header file?


Any of the following function definitions can be placed in a header:


// An inline function
inline void foo()
{
...
}

// A function with static (aka internal) linkage
static void foo()
{
...
}

// A template function
template<typename T>
void foo(T t)
{
...
}


[edit]

For the sake of completeness you can also define a macro function in a header, but I won't dignify that horrible option with an example :)

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!