VS2005新手跪求 include文件问题

avecparapluie 2010-07-20 08:53:11
首次使用VS2005,编译了一个C++小程序(含student.h student.cpp和main.cpp三个文件),出现各种错误...但是在VC6.0上可以通过...跪求VS2005帝赐教!!

// student.h
#include<string>
class Student {
public:
void display();
Student(int num,string name,char sex[6]);
private:
int num;
string name;
char sex[6];
};



//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;
}

Student::Student(int num,string name,char sex[6])
{
this->num=num;
this->name=name;
strcpy(this->sex,sex);
}


//main.cpp
#include "stdafx.h"
#include "student.cpp"

int _tmain(int argc, _TCHAR* argv[])
{
Student stu(1,"sac","male");
stu.display();
return 0;
}

...全文
111 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
avecparapluie 2010-07-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 qinken547 的回复:]
引用 2 楼 avecparapluie 的回复:

引用 1 楼 qinken547 的回复:
C/C++ code
//student.h


#include <string>
#include <iostream>
using namespace std;
class Student {
public:
void display();
Student(int nu……
[/Quote]
要是student.cpp中还有不属于student类中的函数,而main.cpp需要调用,又该怎么办呢?
漫步者、 2010-07-20
  • 打赏
  • 举报
回复
这样是头文件的<>问题,要该成"",还有就是别把类什么的放错位置了。
LZ,自己去试一试看。
王旺旺旺 2010-07-20
  • 打赏
  • 举报
回复
楼主应该还没搞懂声明和定义
qinken547 2010-07-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 avecparapluie 的回复:]

引用 1 楼 qinken547 的回复:
C/C++ code
//student.h


#include <string>
#include <iostream>
using namespace std;
class Student {
public:
void display();
Student(int num,string name,char sex[6]);
……
[/Quote]
晕....第二个文件不是有#include "student.h"这句吗....
student.cpp就是实现student.h里面的声明的...
main.cpp当然应该包函student.h.......
yuhuaijun 2010-07-20
  • 打赏
  • 举报
回复
这是编译器解决的问题,只要包含头文件即可,

每个cpp文件对应编译时对应一个obj文件, 链接时将obj链接到可执行文件,
可以看下《编译原理》
avecparapluie 2010-07-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 qinken547 的回复:]
C/C++ code
//student.h


#include <string>
#include <iostream>
using namespace std;
class Student {
public:
void display();
Student(int num,string name,char sex[6]);
private:
int num;……
[/Quote]
不对吧..Student类的成员函数都在student.cpp中实现的,如果把main中的student.cpp改成student.h,那第二个文件根本没用到....
qinken547 2010-07-20
  • 打赏
  • 举报
回复
//student.h


#include <string>
#include <iostream>
using namespace std;
class Student {
public:
void display();
Student(int num,string name,char sex[6]);
private:
int num;
string name;
char sex[6];
};



//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;
}

Student::Student(int num,string name,char sex[6])
{
this->num=num;
this->name=name;
strcpy(this->sex,sex);
}


//main.cpp

#include "student.h"

int main()
{
Student stu(1,"sac","male");
stu.display();
return 0;
}


用VS2010可以通过 .....
应该是main.cpp文件中#include “student.h”
而不是#include “student.cpp”

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧