在c++里用strcpy()出错,怎么解决

C1401958625 2016-10-22 11:11:39
#include <iostream>
using namespace std;
class Student
{public:
Student(int,char[],char,float);
int get_num(){return num;}
char * get_name(){return name;}
char get_sex(){return sex;}
void display()

{cout<<"num:"<<num<<"\nname:"<<name<<"\nsex:"<<sex<<"\nscore:"<<score<<"\n\n";}
private:
int num;
char name[20];
char sex;
float score;
};
Student::Student(int n,char nam[],char s,float so)
{num=n;
strcpy(name,nam);
sex=s;
score=so;
}
class Teacher
{public:
Teacher(){}
Teacher(Student&);
Teacher(int n,char nam[],char sex,float pay);
void display();
private:
int num;
char name[20];
char sex;
float pay;
};

Teacher::Teacher(int n,char nam[],char s,float p)
{num=n;
strcpy(name,nam);
sex=s;
pay=p;
}
Teacher::Teacher(Student& stud)
{num=stud.get_num();
strcpy(name,stud.get_name());
sex=stud.get_sex();
pay=1500;}
void Teacher::display()
{cout<<"num:"<<num<<"\nname:"<<name<<"\nsex:"<<se x<<"\npay:"<<pay<<"\n\n";}
int main()
{Teacher teacher1(10001,"Li",'f',1234.5),teacher2;
Student student1(20010,"Wang",'m',89.5);
cout<<"student1:"<<endl;
student1.display();
teacher2=Teacher(student1);
cout<<"teacher2:"<<endl;
teacher2.display();
return 0;
}
...全文
1220 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzzzzbin 2019-07-11
  • 打赏
  • 举报
回复
引用 楼主 <span style="color:#4788C7">c1401958625</span>的回复:

编译时出错
<br />加上头文件string.h
hongwenjun 2016-10-25
  • 打赏
  • 举报
回复
添加 #include <string.h>

{cout << "num:" << num << "\nname:" << name << "\nsex:" << sex << "\npay:" << pay << "\n\n";}
这行的 sex 你 输入成 se x了

我是用 CODEBLOCKS的,一个好的IDE 对学习还是很有帮助的


#include <iostream>
#include <string.h>
using namespace std;
class Student
{
public:
Student(int, char[], char, float);
int get_num() {return num;}
char* get_name() {return name;}
char get_sex() {return sex;}
void display()

{cout << "num:" << num << "\nname:" << name << "\nsex:" << sex << "\nscore:" << score << "\n\n";}
private:
int num;
char name[20];
char sex;
float score;
};
Student::Student(int n, char nam[], char s, float so)
{
num = n;
strcpy(name, nam);
sex = s;
score = so;
}
class Teacher
{
public:
Teacher() {}
Teacher(Student&);
Teacher(int n, char nam[], char sex, float pay);
void display();
private:
int num;
char name[20];
char sex;
float pay;
};

Teacher::Teacher(int n, char nam[], char s, float p)
{
num = n;
strcpy(name, nam);
sex = s;
pay = p;
}
Teacher::Teacher(Student& stud)
{
num = stud.get_num();
strcpy(name, stud.get_name());
sex = stud.get_sex();
pay = 1500;
}
void Teacher::display()
{cout << "num:" << num << "\nname:" << name << "\nsex:" << sex << "\npay:" << pay << "\n\n";}
int main()
{
Teacher teacher1(10001, "Li", 'f', 1234.5), teacher2;
Student student1(20010, "Wang", 'm', 89.5);
cout << "student1:" << endl;
student1.display();
teacher2 = Teacher(student1);
cout << "teacher2:" << endl;
teacher2.display();
return 0;
}
C1401958625 2016-10-23
  • 打赏
  • 举报
回复
题目是: 定义一个Teacher(教师)类和一个Student(学生)类,二者有一部分数据成员是相同的,l例如:num(号码),name(姓名),sex(性别)。编写程序,将一个Student对象(学生)转换为Teacher(教师)类,只将以上3个相同的数据成员移植过去。可以设想为: 一位学生大学毕业了,留校担任教师,他原有的部分数据对现在的教师身份来说仍然是有用的,应当保留并成为其教师数据的一部分
C1401958625 2016-10-23
  • 打赏
  • 举报
回复
加了头文件之后还是出错了,我是在Dev上运行的
C1401958625 2016-10-23
  • 打赏
  • 举报
回复

编译时出错
paschen 版主 2016-10-22
  • 打赏
  • 举报
回复 1
包含头文件 <string.h> http://en.cppreference.com/w/c/string/byte/strcpy 如果是在VS中,这个函数是不安全,可能造成缓冲区溢出,所以现在建议用strcpy_s来代替 如果仍想用可以选择以下任何一种方法: 1、新建项目时不要勾选安全开发生命周期检查 2、在项目属性C/C++命令行中加上 /D _CRT_SECURE_NO_WARNINGS
LB__x 2016-10-22
  • 打赏
  • 举报
回复
试试用strcpy_s
raining 2016-10-22
  • 打赏
  • 举报
回复
前面加上 #include <cstring>
ipqtjmqj 2016-10-22
  • 打赏
  • 举报
回复
出错内容是什么,好得描述下
minchieh_fay_ 2016-10-22
  • 打赏
  • 举报
回复
兄弟, 以后提问题,建议描述下 是编译时错误,连接时错误,还是运行时错误 编译时: 添加头文件<string.h> 运行时: 打印下入参看下

65,189

社区成员

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

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