【发布时间】:2013-11-30 18:56:27
【问题描述】:
我有两个 .cpp 文件,一个叫做 practice.cpp,另一个叫做 adder.cpp。它们都在 Source Files 文件夹中。
拳头代码如下:
// adder.cpp
#include "stdafx.h" //include library
int addition(int a, int b) //start function
{
int r = 0; //declare variables
r = a + b; //actual code
return r; //output of function
}
第二个代码:
// Practice.cpp
#include "stdafx.h"
#include "adder.cpp"
#include <iostream>
using namespace std;
int main(void)
{
int number1 = 0;
int number2 = 0;
int number3 = 0;
do
{
printf("\n\nInsert value for first number\n\n");
scanf("%d",&number1);
printf("\nthe value ");
printf("%d ",number1);
printf("has been stored in memory location ");
printf("%d",&number1);
printf("\n\nInsert value for second number\n\n");
scanf("%d",&number2);
printf("\nthe value ");
printf("%d ",number2);
printf("has been stored in memory location ");
printf("%d",&number2);
number3 = addition(number1,number2);
printf("%d",number3);
}
while (1==1);
return 0;
}
但是代码不会编译。我得到了错误:
1>------ Build started: Project: Practice, Configuration: Debug Win32 ------
1> Practice.cpp
1>c:\users\craig\documents\3rd year work\progamable systems\practice\practice\practice.cpp(25): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
1>c:\users\craig\documents\3rd year work\progamable systems\practice\practice\practice.cpp(33): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
1>Practice.obj : error LNK2005: "int __cdecl addition(int,int)" (?addition@@YAHHH@Z) already defined in adder.obj
1>C:\Users\Craig\Documents\3rd year work\Progamable Systems\Practice\Debug\Practice.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
我一直在寻找整个网络,但看起来我做得正确。我能做些什么来解决这个问题?谢谢!
【问题讨论】:
-
不包含 cpp 文件。
-
"already defined in adder.obj" 是告诉你你定义了两次函数,这违反了 ODR。
-
@user34248 您能否将任何答案标记为正确答案? :)