C++ Multiple definition

cheader-files

So I have a header file which has function implementations in it as well as prototypes. I am now trying to separate these implementations from the file and just leave the prototypes since I know its not good standards to have the implementations in header files. However when I remove the implementations from the header file and put them in their own cpp file and then try to link that cpp file to the rest of the project it complains about multiple definitions of the functions. Thanks for any help/advice

std::bitset<LENGTH>  foo1(const std::string stringVal);
std::bitset<LENGTH>  foo2(const int decimalVal);
std::string          foo3(const int integerVal);

I have an include guard on so it doesn't get included more than once as well.

Then in the cpp I simply have my implementations for these functions just straightforward. Yet it complains of multiple definitions of the functions if they are separate from the header file.

EDIT: Stupid mistake on my part this is solved.

Best Answer

That is just a declaration. Even if the file didn't have include guards, it wouldn't give that error.

Multiple definition usually means you're defining the function in more than one implementation file.

Here's a checklist:

  • make sure the implementation is definetely outside of the header.

  • make sure only one source file defines the function

  • make sure you don't include the source file (this one is trivial, I hope you know not to do this)

  • finally, check that you run a clean build

Related Topic