0

I'm too much confused why it's an error to write the same function in two different cpp files?

what if both want to use that function? and why this should ever cause an error, the function is written in to separate files...

a.cpp:

#include "test.h"

b.cpp:

#include "test.h"

test.h

int getMin(int x,int y)
{
    return x;
}

plus, why changing test.h to the following won't fix the problem:

#ifndef UNTITLED1_A_H
#define UNTITLED1_A_H

    int getMin(int x,int y)
    {
        return x;
    }

#endif
  • try with `#pragma once` at top of test.h – pacukluka May 18 '20 at 13:48
  • 1
    @LukaKostic Won't help, the problem is with multiple definition. – Yksisarvinen May 18 '20 at 13:49
  • Does this answer your question? [Why have header files and .cpp files?](https://stackoverflow.com/questions/333889/why-have-header-files-and-cpp-files) – Yksisarvinen May 18 '20 at 13:49
  • @Yksisarvinen doesnt pragma once stop it from being included multiple times and therefore stop it from being defined multiple times? – pacukluka May 18 '20 at 13:50
  • 2
    @LukaKostic `#pragma once` will prevent you from including a file twice in one translataion unit (a.k.a. `.cpp` file). But include guards like in last snippet would do the same. The problem is that this definition is included in two translations units, which means double definition. – Yksisarvinen May 18 '20 at 13:52

2 Answers2

0

If you define a function twice, the linker will see 2 definitions of the same function, which is not allowed. (even though each .cpp that includes this header file sees only one definition, which is why adding header guards doesn't help here).

One option is to only write the declaration of the function in the .h file, and write the definition in a separate .cpp file. (this is the more common implementation).

If you want to define the function inside the header file, then it needs to be inline, like this:

inline int getMin(int x,int y)
    {
        return x;
    }

Note that if you do the second option, then all files that include this header will need to be recompiled, if you change the internals of this function (this is probably not a good idea, when the interface doesn't change).

cigien
  • 57,834
  • 11
  • 73
  • 112
  • why using inline will fix this, but I read that sometimes even though I wrote inline the compiler would ignore that, so in this condition why doesn't this cause an error? –  May 18 '20 at 13:58
  • I think you are thinking about `inline` as an optimization. But here it simply means, this is defined once across the entire program. – cigien May 18 '20 at 14:03
  • @john1998 That's about a different thing. `inline` keyword in the past was a suggestion to compiler that it should replace calls to the function with the body of the function. This is pretty much ignored now. But `inline` also means "Please ignore One Definition Rule on this thing, I guarantee that all definitions will be the same". It lets you provide multiple definitions, but it's your responsibility to make sure each and every definition is the same (and you're dead if someone ever creates a function with the same name and same parameter list, but different body). – Yksisarvinen May 18 '20 at 14:03
0

You need to make this function "inline" and it will work.

Dragan
  • 66
  • 5
  • While that will work that is definetly not what he should do in this case. – Eric May 18 '20 at 13:57
  • why using inline will fix this, but I read that sometimes even though I wrote inline the compiler would ignore that, so in this condition why doesn't this cause an error? –  May 18 '20 at 13:59
  • @Eric why, could you elaborate? –  May 18 '20 at 14:00
  • If we are talking about what he should do, it is to use std::min. Otherwise, inlining a function is a valid way to have it in a header. There are two purposes for inline - one is a hint to the compiler to generate the code inside the calling function; the other is to allow the function to appear in different compilation units without conflict (in layman terms). – Dragan May 18 '20 at 14:02
  • @john1998 Cigien explained it in his answer. If you have the definition in you header everything using this header needs to be recompiled when you change your function. A getMin function probably won't change frequently so in this particular case it's fine. But generally speaking when you want to share functions across you program it's preferable to use first option from cigien's answer. – Eric May 19 '20 at 11:17