C++源程序编译没问题、连接出错误

来源:百度知道 编辑:UC知道 时间:2024/06/11 06:07:54
我使用的开发环境是VS 2005。我在Solution1中的一个名字为Project1 中的头文件(MyFunc.h)里面声明了一个函数void MyFunc(int =0);,在Solution2中的Project1中include 了这个头文件(MuFunc.h),主程序调用MyFunc(7),C++源程序编译没问题、连接出错误“fatal error LNK1120: 1 个无法解析的外部命令”,仔细察看,连接器把MyFunc解析为“MyFunc@@QAEHHQAPAD@Z”。请高手予以指点迷津。
工程proj1的源代码:

//自定义头文件assistant.h
#ifndef ASSITANT_H
#define ASSITANT_H

int fnSwap(int*, int*);

#endif

//自定义assistant.CPP
#include "assistant.h"

int fnSwap(int* a, int* b)
{
return 0;
}

//主程序:proj1.cpp
#include <iostream>
#include "assistant.h"
using namespace std;

int main()
{
int i=5,j=8;
fnSwap(&i,&j);
return 0;
}

//工程2:
//主程序proj2.cpp
#include <iostream>
#include "..\Proj1\assistant.h"
using namespace std;

int main()
{
int i=5,j=8;
f

文件包含要放在当前Project文件下,或者指出文件包含路径.

这是典型的只有声明,没有定义的错误
你的int MyFunc(int = 0) 没有实现,当然调用的时候会出错了。
我不都说了嘛,你的.h文件中只有函数的声明,没有定义,proj2虽然包含了assistant.h,但是这个头文件中并不包含fnSwap的具体实现,当然会出错了,除非你把assistant.CPP的实现也放到assistant.h中,才可以。