鱼C论坛

 找回密码
 立即注册
查看: 5360|回复: 12

在头文件中用string类来定义出错

[复制链接]
发表于 2012-8-1 15:59:08 | 显示全部楼层 |阅读模式
5鱼币
主函数文件
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. #include "1.h"

  5. main()
  6. {
  7.         graduate stu_name;
  8.         stu_name.gra_input();
  9.         stu_name.gra_display();
  10.         return 0;
  11. }
复制代码
头文件
  1. #include<string.h>

  2. using namespace std;

  3. class student
  4. {
  5. public:
  6.         void stu_input();
  7. protected:
  8.         string name
  9.         int age;
  10.         float score;
  11. };

  12. class teacher
  13. {
  14. public:
  15.         void tea_input();
  16. protected:
  17.         string name;
  18.         string sex;
  19.         string title;
  20. };

  21. class graduate:public student, public teacher
  22. {
  23. public:
  24.         void gra_input();
  25.         void gra_display();
  26. private:
  27.         float money;
  28. };
复制代码

定义函数文件
  1. #include<iostream>
  2. #include<string>
  3. #include "1.h"

  4. using namespace std;

  5. void student::stu_input()
  6. {
  7.         cout<<"请输入学生姓名:";
  8.         cin>>name;
  9.         cout<<"请输入学生年龄:";
  10.         cin>>age;
  11.         cout<<"请输入学生分数:";
  12.         cin>>score;
  13. }

  14. void teacher::tea_input()
  15. {
  16.         cout<<"请输入教师姓名:";
  17.         cin>>name;
  18.         cout<<"请输入教师性别:";
  19.         cin>>sex;
  20.         getchar();
  21.         cout<<"请输入教师职称:";
  22.         cin>>title;
  23. }

  24. void graduate::gra_input()
  25. {
  26.         stu_input();
  27.         tea_input();
  28.         cout<<"请输入研究生薪水:";
  29.         cin>>money;
  30. }

  31. void graduate::gra_display()
  32. {
  33.         cout<<"=====研究生信息====="<<endl;
  34.         cout<<"姓名:"<<student::name<<endl;
  35.         cout<<"年龄:"<<age<<endl;
  36.         cout<<"分数:"<<score<<endl;
  37.         cout<<"性别:"<<sex<<endl;
  38.         cout<<"职称:"<<title<<endl;
  39.         cout<<"薪水:"<<money<<endl<<endl;
  40. }

复制代码

最佳答案

查看完整内容

我这样搞的VC 6.0->Win32 Console Application->一个空的工程->新建2个C++源文件,1个头文件,把你的代码复制进去就好了
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-8-1 15:59:09 | 显示全部楼层
我这样搞的VC 6.0->Win32 Console Application->一个空的工程->新建2个C++源文件,1个头文件,把你的代码复制进去就好了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-8-1 16:21:18 | 显示全部楼层
头文件的第八行string name后缺少“;”
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-8-1 16:26:34 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-8-1 16:27:17 | 显示全部楼层

  1. class student
  2. {
  3. public:
  4.         void stu_input();
  5. protected:
  6.         string name;
  7.         int age;
  8.         float score;
  9. };

  10. class teacher
  11. {
  12. public:
  13.         void tea_input();
  14. protected:
  15.         string name;
  16.         string sex;
  17.         string title;
  18. };

  19. class graduate:public student, public teacher
  20. {
  21. public:
  22.         void gra_input();
  23.         void gra_display();
  24. private:
  25.         float money;
  26. };
复制代码
重新发下头文件的代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-8-1 17:12:51 | 显示全部楼层
编译通过,正常运行的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-8-1 17:16:13 | 显示全部楼层
编译没错 但是在运行的时候就会出错误
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-8-1 17:53:03 | 显示全部楼层
丿夏夜灬彬刂 发表于 2012-8-1 17:16
编译没错 但是在运行的时候就会出错误

运行了,就输入一些数据,然后输出一些数据,再程序就结束了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-8-1 18:15:52 | 显示全部楼层
你的是什么编译器咯 为什么我的编译器 不行了 ?:o
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-8-1 18:25:33 | 显示全部楼层
五月跳回家 发表于 2012-8-1 18:23
我这样搞的VC 6.0->Win32 Console Application->一个空的工程->新建2个C++源文件,1个头文件,把你的代码复 ...

对啊 我也是这样的啊 但是编译不会出错误 一运行就不行了  问题就是出在 头文件 用string 定义 name 这里出错
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-8-1 18:32:53 | 显示全部楼层
本帖最后由 五月跳回家 于 2012-8-1 18:34 编辑


:funk::funk::funk:,0 error(s), 0 warning(s)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2012-8-1 18:44:30 | 显示全部楼层
我也不知道到底是怎么回事 有人能解释下嘛 编译的时候没错误 但是运行就会报错
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-8-1 22:05:04 | 显示全部楼层
好好弄吧嘿嘿。细心点就会好了。这个最大的bug就是输入上没有处理好。希望你能自己运行的时候就知道了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-12 04:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表