菜鸟问题,有关.h .cpp和stdafx.h
编程环境是vc 2005,当我创建了一个Win32 控制台应用程序的项目,命名为test_pro之后,自动出现了一个头文件 stdafx.h,两个源文件stdafx.cpp和test_pro.cpp.
然后我添加了一个源文件add2.cpp:
#include "stdafx.h"
int add2(int a, int b)
{
int n=a+b;
return n;
}
再添加了一个头文件add2.h:
#include "stdafx.h"
#include "add2.cpp"
int add2(int x,int y);
之后在test_pro.cpp中写:
#include "stdafx.h"
#include "add2.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int c=add2(3,4);
cout<<c;
}
调试报错:add2.obj : error LNK2005: "int __cdecl add2(int,int)" (?add2@@YAHHH@Z) 已经在 test_pro.obj 中定义
1>C:\Documents and Settings\jfqian\My Documents\Visual Studio 2005\Projects\test_pro\Debug\test_pro.exe : fatal error LNK1169: 找到一个或多个多重定义的符号
好多困惑哦:
1. 头文件的作用不就是告诉vc,我要include 那些东西么?那么我可不可以直接在sdafx.h中直接加
#include "add2.cpp"
int add2(int x,int y);
然后在主程序文件中直接include sdafx.h就好了,另外还要弄个什么add2.h是不是有些多余啊。
2.stdafx.cpp这个文件是系统直接生成的,我看它里面除了一句#include "stdafx.h"什么都没有啊,到底有什么用哦?我可以不可以把函数的实现写到里面呢?这样就不用
add2.cpp了。
3.最后的错误又是什么意思?
谢谢指教!