为什么构造函数里的参数不能用字符串,要怎样用字符串做参数(“老师”那个参数)

禾下月 2018-11-20 10:48:48
//5.5
#include<iostream>
#include<string>
#include<string.h>
using namespace std;

class School
{
private:
string object;
string name;
int age;
int numeber;
int sex;
public:
School(string &object1,string &name1, int &age1, int &numeber1, char sex1)
{
if (sex1 == 'm')
sex = 0;
if (sex1 == 'f')
sex = 1;
age = age1;
numeber = numeber1;
name = name1;
object =object1;
}
int printf();
};
int School::printf()
{
cout << "身份:" << object << endl;
cout <<"姓名:"<<name<<endl;
cout << "年龄: " << age<<endl;
if (sex == 0)
cout << "性别:男" << endl;
if (sex ==1)
cout<<"性别:女:"<<endl;
cout << "numeber:" << numeber << endl;
}

class Student :public School
{
private:
int ave_gar; //平均成绩
public:
Student(string &object1, string &name1, int &age1, int &numeber1, char sex1,int ave_gar1) :School
(object1, name1, age1, numeber1,sex1), ave_gar(ave_gar1)
{
cout << "平均成绩:" << ave_gar << endl;
}
};
class Teacher :public School
{
private:
string teacher; //教授的学科
public:
Teacher(string &object1, string &name1, int &age1, int &numeber1, char sex1, string &teacher1) :School
(object1,name1,age1,numeber1,sex1), teacher(teacher1)
{
cout << "教授的学科:" << teacher << endl;
}
};
class Staff :public School
{
private:
string work;
Staff(string &object1, string &name1, int &age1, int &numeber1, char sex1,string &work1):School
(object1, name1, age1,numeber1, sex1),work(work1)
{
cout << "工作类型:" << work<<endl;
}
};

int main()
{
Teacher a("教师","王一",32,2017320,'f',72);
a.printf();
Student b("学生","李二",15,2017203,'m',"语文");
b.printf();
Staff c("职工","赵三",28,2017256,'f',"清洁");
c.printf();
return 0;
}
...全文
811 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
alibaren 2018-12-30
  • 打赏
  • 举报
回复
#include<iostream>
#include<string>
#include<string.h>
using namespace std;

class School
{
private:
	string object;
	string name;
	int age;
	int numeber;
	char sex;
public:
	School(const string& object1,const string& name1, const int& age1, const int& numeber1, char sex1)
	{
		if (sex1 == 'm')
		sex = 0;
		if (sex1 == 'f')
		sex = 1;
		age = age1;
		numeber = numeber1;
		name = name1;
		object =object1;
	}
	void printf();
};
	
void School::printf()
{
	cout << "身份:" << object << endl;
	cout <<"姓名:"<<name<<endl;
	cout << "年龄: " << age<<endl;
	if (sex == 0)
	cout << "性别:男" << endl;
	if (sex ==1)
	cout<<"性别:女:"<<endl;
	cout << "numeber:" << numeber << endl;
}

class Student :public School
{
private:
int ave_gar;	//平均成绩
public:
Student(const string& object1, const string& name1, const int& age1, const int& numeber1, char sex1,int ave_gar1) :School
(object1, name1, age1, numeber1,sex1), ave_gar(ave_gar1)
{
cout << "平均成绩:" << ave_gar << endl;
}
};
class Teacher :public School
{
private:
string teacher;	//教授的学科
public:
Teacher(const string& object1,const string& name1, const int&  age1, const int&  numeber1, char sex1, string teacher1) :School
(object1,name1,age1,numeber1,sex1), teacher(teacher1)
{
cout << "教授的学科:" << teacher << endl;
}
};
class Staff :public School
{
private:
string work;
public:
Staff(const string& object1, const string& name1, const int& age1, const int& numeber1, char sex1,string work1):School
(object1, name1, age1,numeber1, sex1),work(work1)
{
  cout << "工作类型:" << work<<endl;
}
};

int main()
{
	Teacher a("教师","王一",(const int&)32,(const int&)2017320,'f',"History");
	a.printf();
	Student b("学生","李二",(const int&)15,(const int&)2017203,'m',45);
	b.printf();
	Staff c("职工","赵三",(const int&)28,(const int&)2017256,'f',"清洁");
	c.printf();
	return 0;
}
wersdfadaf 2018-12-29
  • 打赏
  • 举报
回复 2
const string&
如果你是c++新手就只要记得这么用就好了,
如果不是新手就去看看C++标准const &的作用。简单点说,它会延长对象的生命周期。
cwenhe0324 2018-12-29
  • 打赏
  • 举报
回复
引用 17 楼 cwenhe0324 的回复:
这个问题是因为你构造函数是用引用的需要的是左值类型,而你传的字符串是右值,把所有引用改成const&就可以了
例 Teacher(string const &object1, string const &name1, int const &age1, int const&numeber1, char sex1, string const&teacher1), 将所有的构造函数都修改了就没问题了
cwenhe0324 2018-12-29
  • 打赏
  • 举报
回复
这个问题是因为你构造函数是用引用的需要的是左值类型,而你传的字符串是右值,把所有引用改成const&就可以了
Xyino_Snake 2018-12-20
  • 打赏
  • 举报
回复
7楼方法是可行的。多写一个构造函数即可。
Teacher(const char * Name...):Teacher(string(Name)...){ ... }
我这个是伪代码,应该看得懂的。
其实就是C字符串转换为string而已。
禾下月 2018-11-26
  • 打赏
  • 举报
回复
引用 13 楼 lianshaohua 的回复:
[quote=引用 11 楼 XN已被占用 的回复:] [quote=引用 10 楼 lianshaohua 的回复:] 把所有的string &修改为:const string &;或改成:const char *,或改成string &&(c++11)
还是不行,怎么改呀?[/quote] 什么现象?[/quote] 严重性 代码 说明 项目 文件 行 禁止显示状态 错误 C2664 “Teacher::Teacher(Teacher &&)”: 无法将参数 3 从“int”转换为“int &” Project1 c:\users\shinelon\desktop\project1\project1\源.cpp 80
赵4老师 2018-11-26
  • 打赏
  • 举报
回复
string teacher =string("教师");
ztenv 版主 2018-11-26
  • 打赏
  • 举报
回复
引用 11 楼 XN已被占用 的回复:
[quote=引用 10 楼 lianshaohua 的回复:]
把所有的string &修改为:const string &;或改成:const char *,或改成string &&(c++11)

还是不行,怎么改呀?[/quote]
什么现象?
weixin_43652323 2018-11-25
  • 打赏
  • 举报
回复
我觉得应该是函数参数不能直接传字符串内容,字符串应该先定义再使用,否则编译器不知该如何分配存储空间,改法如二楼。
禾下月 2018-11-25
  • 打赏
  • 举报
回复
引用 10 楼 lianshaohua 的回复:
把所有的string &修改为:const string &;或改成:const char *,或改成string &&(c++11)
还是不行,怎么改呀?
ztenv 版主 2018-11-23
  • 打赏
  • 举报
回复
把所有的string &修改为:const string &;或改成:const char *,或改成string &&(c++11)
  • 打赏
  • 举报
回复
用引用可以的,是对的啊
真相重于对错 2018-11-22
  • 打赏
  • 举报
回复
string 代替 string& int 代替int&
smwhotjay 2018-11-21
  • 打赏
  • 举报
回复
“老师” 这个 是char*
真相重于对错 2018-11-21
  • 打赏
  • 举报
回复
引用 2 楼 ForgetTomorrow的回复:
用引用没问题,而且比值传递更好。
改成这样就行了:
string teacher ="教师"
string name = "王一"
...
Teacher a(teacher ,name ...);
用引用是没问题,但楼主问的是为何不能直接用字面值
ForgetTomorrow 2018-11-21
  • 打赏
  • 举报
回复
用引用没问题,而且比值传递更好。
改成这样就行了:
string teacher ="教师"
string name = "王一"
...
Teacher a(teacher ,name ...);
真相重于对错 2018-11-21
  • 打赏
  • 举报
回复
用引用不对,改成值类型。
Ps,用引用做参数,要把引用是什么搞清楚。
禾下月 2018-11-21
  • 打赏
  • 举报
回复
引用 3 楼 hdt 的回复:
[quote=引用 2 楼 ForgetTomorrow的回复:]用引用没问题,而且比值传递更好。 改成这样就行了: string teacher ="教师" string name = "王一" ... Teacher a(teacher ,name ...);
用引用是没问题,但楼主问的是为何不能直接用字面值[/quote] //5.5 #include<iostream> #include<string> #include<string.h> using namespace std; class School { private: char object[20]; char name[20]; int age; int numeber; int sex; public: School( char * object1, const char * name1, int &age1, int &numeber1, char sex1) { if (sex1 == 'm') sex = 0; if (sex1 == 'f') sex = 1; age = age1; numeber = numeber1; strcpy(object,object1 ); strcpy(name, name1); } int printf(); }; int School::printf() { cout << "身份:" << object << endl; cout <<"姓名:"<<name<<endl; cout << "年龄: " << age<<endl; if (sex == 0) cout << "性别:男" << endl; if (sex ==1) cout<<"性别:女:"<<endl; cout << "numeber:" << numeber << endl; } class Student :public School { private: int ave_gar; //平均成绩 public: Student( char * object1, char * name1, int &age1, int &numeber1, char sex1,int ave_gar1) :School (object1, name1, age1, numeber1,sex1), ave_gar(ave_gar1) { cout << "平均成绩:" << ave_gar << endl; } }; class Teacher :public School { private: char teacher[20]; //教授的学科 public: Teacher( char * object1, char * name1, int &age1, int &numeber1, char sex1, char * teacher1) :School (object1,name1,age1,numeber1,sex1) { strcpy(teacher, teacher1); cout << "教授的学科:" << teacher << endl; } }; class Staff :public School { private: char work[20]; Staff( char * object1, char * name1, int &age1, int &numeber1, char sex1,char * work1):School (object1, name1, age1, numeber1, sex1) { strcpy(work,work1); cout << "工作类型:" << work<<endl; } }; int main() { Teacher a("教师","王一",32,2017320,'f',72); a.printf(); Student b("学生","李二",15,2017203,'m',"语文"); b.printf(); Staff c("职工","赵三",28,2017256,'f',"清洁"); c.printf(); return 0; } 改成这样也不行
禾下月 2018-11-21
  • 打赏
  • 举报
回复
引用 5 楼 yunchao630 的回复:
改成这样试试 Teacher(const string &object1,const string &name1, int &age1, int &numeber1, char sex1, const string &teacher1)
引用 5 楼 yunchao630 的回复:
改成这样试试 Teacher(const string &object1,const string &name1, int &age1, int &numeber1, char sex1, const string &teacher1)
不行
翅膀又硬了 2018-11-21
  • 打赏
  • 举报
回复
改成这样试试
Teacher(const string &object1,const string &name1, int &age1, int &numeber1, char sex1, const string &teacher1)

64,637

社区成员

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

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