-1

When I tried to run the program, visual studio showed the errors LNK2005 and LNK1169.

I created two source files and I use #include in main file. Could someone tell me why it is wrong

Here is my code in 1.5.3.cpp

#include "stdafx.h"
#include "Print.cpp"

    int main()
{
    double b;
    printf("Input a number\n");
    scanf_s("%lf\n", &b);
    print(b);//call print()
    return 0;
}

And here is my code in Print.cpp

#include "stdafx.h"

void print(double a)
{
    double c = a * 2;
    printf("%lf multyply by 2 is %lf", a, c);

}

enter image description here

rollstuhlfahrer
  • 3,762
  • 9
  • 21
  • 37
Gordon
  • 13
  • 3

1 Answers1

0

This is your problem:

#include "Print.cpp"

This defines the function void print(double a) in both the Print.cpp file AND the 1.5.3.cpp file. When the 1.5.3.cpp file is compiled it produces a symbol void print(double a) in the 1.5.3.obj file. Similarly, when the Print.cpp file is compiled, it also produces a symbol void print(double a) in the Print.obj file. Thus the linker errors:

LNK2005 "void __cdecl print(double)" (?print@@YAXN@Z) already defined in 1.5.3.obj LNK1169 one or more multiply defined symbols found

You need to declare void print(double a) in a Print.h file and #include it in the 1.5.3.cpp and Print.cpp files.

You may want to see: What is the difference between a definition and a declaration?

MFisherKDX
  • 2,741
  • 3
  • 11
  • 25