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

新手c++头文件链接问题

Corro 发布于 2007-10-11 09:32, 655 次点击

就是谭浩强那本c++中的252页中的一个例题
//student.h
class student
{
public:
void display();

private:
int num;
char name[20];
char sex;
};
//student.cpp

#include <iostream>
#include "student.h"
using namespace std;
void student::display()
{
cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
//main.cpp
#include<iostream>
#include"student.h" (问题就在这原书是包含了student.h文件,我在编译时是正确的可在链接是有一个错误,把它 改为student.cpp就可以了,不知道什么原因那位给解释一下 谢谢了)

void main()

{
student s;
s.display();

}

11 回复
#2
Corro2007-10-11 21:46
#3
vgkvgk2007-10-11 21:49

不懂,帮顶,我要成为游民....

#4
liubillye2007-10-11 23:02
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
#5
liubillye2007-10-11 23:03
nnnnnnnnnnnnnnnnnnnnnnnn
#6
liubillye2007-10-11 23:03
jhhhhbv
#7
liubillye2007-10-11 23:03
fdcfgfhgjhg
#8
liubillye2007-10-11 23:03
544444444444465
#9
liubillye2007-10-11 23:04
6789999999999999999
#10
凉拌粉丝2007-10-11 23:05
额也不懂
#11
kingkong63582007-10-11 23:07
我看不懂啊!!!
#12
discus8152007-10-12 08:56
//main.cpp
#include<iostream>
#include"student.h"
这个里面的"student.h"并不包含函数的定义,只是函数的声明,main 要通过,就要调用"student.cpp"
而student.cpp就一定要引用"student.h"

这些问题会随着你不断地接触C++语言,逐渐理解的
1