0

I have tried using a template in a class but it works in the main class.

Here is my main class:

#include "AddSubtract.cpp"
#include <iostream>
#include <string>

using namespace std;

int main() {
    templEx();
}

And here is the class where the template is located:

#include <iostream>

#include <string>

using namespace std;

template<typename T>
void Print(T value)
{
    std::cout << value << std::endl;
}

void templEx() {
    Print("Venezuela");
}

And here are the errors which I get:

Severity Code Description Project File Line Source Suppression State Error LNK2005 "void __cdecl templEx(void)" (?templEx@@YAXXZ) already defined in AddSubtract.obj ConsoleApplication1 C:\Users\bahge\source\repos\ConsoleApplication1\ConsoleApplication1\Source1.obj 1 Build

Severity Code Description Project File Line Source Suppression State Error LNK1169 one or more multiply defined symbols found ConsoleApplication1 C:\Users\bahge\source\repos\ConsoleApplication1\x64\Debug\ConsoleApplication1.exe 1 Build

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
BahiGoz
  • 47
  • 4
  • 1
    Note: That's not the main class, that's the `main` *function*. – tadman Aug 05 '22 at 20:06
  • 3
    Tip: Don't `#include` `.cpp` files. It messes a lot of things up, like precisely this. You should only ever have to include header files, typically `.h` though `.hpp` is also used. – tadman Aug 05 '22 at 20:06
  • OK I wil edit the question – BahiGoz Aug 05 '22 at 20:06
  • I will fix that @tadman – BahiGoz Aug 05 '22 at 20:08
  • You need to move the template definition to a header file and `#include` it or the `main()` function will not know what you're talking about. – tadman Aug 05 '22 at 20:08
  • 1
    Do you still have the same error with this recently-edited code? Your error looks like something that would result from including a cpp file, and your revised main file looks like it would produce different errors now.. – Drew Dormann Aug 05 '22 at 20:10
  • Add this to main.cpp `extern void templEx();` – Eljay Aug 05 '22 at 20:19
  • I have rolled back the edit to this question so the code, the error, and the accepted answer once again match. – Drew Dormann Aug 05 '22 at 21:02

1 Answers1

1

You're compiling that code that defines tmplEx once in the main.cpp and additionally in the secondary source file, which leads to the conflict.

Define the template in a separate header file that both can #include as necessary.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • OK I will fix that and check if it fixes the issue – BahiGoz Aug 05 '22 at 20:12
  • Making `templEx` `inline` would be an alternative... – fabian Aug 05 '22 at 20:16
  • @fabian A messy solution to an otherwise simple fix. – tadman Aug 05 '22 at 20:19
  • You could say the same about introducing another source file or about possibly taking away a possibility for the compiler to optimize out such a simple function... Not saying your solution isn't good, but there ***are*** scenarios where a `inline` function should be chosen. – fabian Aug 05 '22 at 20:25
  • @fabian For sure, `inline` is helpful in some situations, but this function is trivial and does not need such treatment. It's best left in a source file and compiled separately, and doing that habitually leads to far less cluttered code. – tadman Aug 05 '22 at 20:26