gpt4 book ai didi

在 Visual Studio 2017 中编译 C 代码错误 LNK2005 和 LNK1169

转载 作者:太空宇宙 更新时间:2023-11-04 00:58:20 24 4
gpt4 key购买 nike

我在编译计算正整数总和的代码时遇到问题。令我困惑的是,我设法在在线 C 编译器 (https://www.onlinegdb.com/online_c_compiler) 上成功编译和运行代码,但在 VS2017 上出现 LNK2005 和 LNK1169 错误。如何解决?

//Calculate total sum of positive integer.

#include <stdio.h>

int sum(int n);

int main(void) {
int n;
printf("Enter positive value of integer: ");
scanf("%d", &n);
printf("\nTotal value for %d is = %d\n", n, sum(n));
return (0);
}

int sum(int n) {
if (n == 0) return 0;
else return (n*(n+1)/2);
}

对了,请忽略VS2017上的scanf警告,稍后我会把它改回scanf_s。

最佳答案

如果您有多个包含 main 函数的源文件,则会发生这种常见错误。

关于在 Visual Studio 2017 中编译 C 代码错误 LNK2005 和 LNK1169,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50869532/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com