C++类的一个简单问题
SUM.H
#include<iostream>
#include<string>
using namespace std;
class he
{public:
int a,b;
int qiuhe(int a,int b);
};
SUM.CPP
#include"sum.h"
int he::qiuhe(int a,int b)
{//int a=1,b=2;
return a+b;
}
MAIN.CPP
#include"sum.h"
int qiuhe(int a,int b);
int main()
{
int a=0,b=0,c=0;
c=qiuhe(a,b);
}
我是菜鸟,为什么会编译不通过啊?
(错误1error LNK2019: 无法解析的外部符号 "int __cdecl qiuhe(int,int)" (?qiuhe@@YAHHH@Z),该符号在函数 _main 中被引用D:\代码\first\wocao\wocao\main.objwocao
)
(错误2error LNK1120: 1 个无法解析的外部命令D:\代码\first\wocao\Debug\wocao.exe11wocao
)
[解决办法]
#include<iostream>#include<string>using namespace std; class he{public:int a,b;int qiuhe(int a,int b);};int he::qiuhe(int a,int b){//int a=1,b=2;return a+b;}int main(){ he you;int a=0,b=0,c=0;c=you.qiuhe(a,b);}
[解决办法]
int qiuhe(int a,int b); 此句只是函数申明,并没有定义
c=qiuhe(a,b);导致此句不可执行
另外类的成员函数一般都是通过对象去调用的(或者直接通过类),直接调用编译器不知道是在哪个类定义的 自然不能通过编译。