注册 登录
编程论坛 新人交流区

VC中 Error LNK2001的问题

vampr 发布于 2007-10-17 13:36, 291 次点击

小弟刚刚学习C++,比较菜,以前一直用Gcc写,现在老师强制要求用VC写
我的编译环境是VC++6.0,随便写了一个程序,想试试,结果可以编译,但是无法链接
在Gcc里是可以链接的,小弟不明白,发上来请高手指点指点,小弟无比感激了
#include <iostream>
using namespace std;
int max(int a,int b);
long max(long a,int b);
double max(double a,double b);
int main()
{
int x1,y1;
long x2,y2;
double x3,y3;
cout<<"x1="<<endl;
cin>>x1;
cout<<"y1="<<endl;
cin>>y1;
cout<<"max="<<max(x1,y1)<<endl;
cout<<"x2="<<endl;
cin>>x2;
cout<<"y2="<<endl;
cin>>y2;
cout<<"max="<<max(x2,y2)<<endl;
cout<<"x3="<<endl;
cin>>x3;
cout<<"y3="<<endl;
cin>>y3;
cout<<"max="<<max(x3,x3)<<endl;
return 0;
}


int max(int a,int b)
{
return(a>b?a:b);
}

long max(long a,long b)
{
return(a>b?a:b);
}

double max(double a,double b)
{
return(a>b?a:b);
}

VC提示的错误:
Linking...
1.obj : error LNK2001: unresolved external symbol "long __cdecl max(long,int)" (?max@@YAJJH@Z)
Debug/1.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

1 回复
#2
longfeng8672007-10-17 14:07

在 VC运行环境中,函数必须先声明后调用,如果有同名函数(自定义 函数同名,跟库函数同名),
这时候可以改函数名称,或者像这样写,把主函数写在最后:

#include <iostream>
using namespace std;
int max(int a,int b)
{
return(a>b?a:b);
}

long max(long a,long b)
{
return(a>b?a:b);
}

double max(double a,double b)
{
return(a>b?a:b);
}
int main()
{
int x1,y1;
long x2,y2;
double x3,y3;
cout<<"x1="<<endl;
cin>>x1;
cout<<"y1="<<endl;
cin>>y1;
cout<<"max="<<max(x1,y1)<<endl;
cout<<"x2="<<endl;
cin>>x2;
cout<<"y2="<<endl;
cin>>y2;
cout<<"max="<<max(x2,y2)<<endl;
cout<<"x3="<<endl;
cin>>x3;
cout<<"y3="<<endl;
cin>>y3;
cout<<"max="<<max(x3,x3)<<endl;
return 0;
}

[此贴子已经被作者于2007-10-17 14:08:48编辑过]

1