Function definitions and LNK2005

Started by
2 comments, last by Mizipzor 15 years, 9 months ago
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?
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.
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 functioninline void foo(){   ...}// A function with static (aka internal) linkagestatic void foo(){   ...}// A template functiontemplate<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 :)
Thanks, added inline and it works as intended now. :)

This topic is closed to new replies.

Advertisement