Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c程序中的错误LNK2005_C - Fatal编程技术网

c程序中的错误LNK2005

c程序中的错误LNK2005,c,C,当我尝试运行该程序时,visual studio显示了错误LNK2005和LNK1169 我创建了两个源文件,并在主文件中使用了#include。有人能告诉我为什么错了吗 这是我在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

当我尝试运行该程序时,visual studio显示了错误
LNK2005
LNK1169

我创建了两个源文件,并在主文件中使用了
#include
。有人能告诉我为什么错了吗

这是我在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;
}
这是我在Print.cpp中的代码

#include "stdafx.h"

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

}
这是你的问题:

#include "Print.cpp"
print.cpp
文件
1.5.3.cpp
文件中定义了
void print(双a)
功能。编译
1.5.3.cpp
文件时,它会在1.5.3.obj文件中生成一个符号
void print(双a)
。类似地,编译
Print.cpp
文件时,它还会在Print.obj文件中生成一个符号
void Print(双a)
。因此,链接器错误:

LNK2005“作废cdecl打印(双联)”(?打印@@YAXN@Z)已在1.5.3.obj中定义 LNK1169找到一个或多个多重定义符号

您需要
print.h
文件中声明
无效打印(双a)
,并将其包含在
1.5.3.cpp
print.cpp
文件中

您可能希望看到:

在问题中包含错误信息可能会有所帮助。那么VS只提供了文本错误LNK2005和LNK1169?没有其他信息吗?没有文字描述,没有行号,没有代码,除了错误LNK2005和LNK1169什么都没有?我觉得这有点难以置信。错误信息就在你面前的屏幕上;为什么他们没有出现在你的帖子中,我们也有这些信息?为什么文件是
*.cpp
时使用C标记?@Gordon:你没有“Print.h”头文件吗?@WeatherVane:VS默认所有源文件为
*.cpp
。如果需要
.c
文件,则必须重命名它们。