以字符串为参数的函数 / 不同形式字符串的使用

本文总结了C++中以字符串为参数的函数的三种形式:字符指针、字符数组和string类型。讨论了字符指针使用时的E0167错误及其解决方案,解释了const在函数参数中的作用。还提到了字符数组的使用,以及string类型的便捷性。文章通过实例分析了不同字符串类型在面向对象编程中的应用。
摘要由CSDN通过智能技术生成

面向对象上机,函数参数里面最常用到的应该就是字符串了,但是一直都没有搞清楚具体有哪些使用形式,怎么用,对使用时所会发生的错误有哪些解决办法?于是乎在今天做个总结,方便自己日后回顾。
Ⅰ.字符指针
1.常见错误与处理方法
(1)E0167 “const char *” 类型的实参与 “char *” 类型的形参不兼容
比如下面这段代码

/*定义student类,包含姓名,学号,分数,三个数据成员;有三个成员函数:设置学生信息,显示学生信息,
比较两个学生信息是否完全相同(可以使用一个对象,或一个对象指针,或一个对象对象引用,作为形参);
为student类编写常规构造和拷贝构造函数;编写析构函数。主程序中创建两个学生对象,一个常规构造,
一个拷贝构造;然后分别设置两个学生信息,再比较两个学生信息是否相同并输出结果。*/
#include<iostream>
using namespace std;
class student
{
   
private:
    char *name;
    long int num;
	int score;
		 
public:
	student(char *Name=NULL ,long int Num=0 ,int Score =0 )   //带默认形参的构造函数
	{
   
		name=Name;
		num=Num;
		score=Score;
		cout<<"构造成功!"<<endl;
	}
	~student()                                               //析构函数
	{
   
		cout<<"析构成功!"<<endl;
	}
	student(student *p)                                     //拷贝构造函数
	{
   
		name=p->name;
		num=p->num;
		score=p->score;
		cout<<"拷贝构造成功!"<<endl;
	}
	void write(char *Name,long int Num,int Score)          //设置学生信息
	{
   
		name=Name;
		num=Num;
		score=Score;
		cout<<"设置学生信息成功!"<<endl;
	}
	void showinfo()                                       //显示学生信息
	{
   
		cout<<name<<" "<<num<<" "<<score<<endl;
	}
	void compare(student a, student b)                   //比较学生信息是否相同 
	{
   
		if(a.num==b.num)
			cout<<"学号相同"<<endl;
		else cout<<"学号不相同"<<endl;
		if(a.score==b.score)
			cout<<"分数相同"<<endl;
		else cout<<"分数不相同"<<endl;
		if(strcmp(a.name,b.name)==0)
			cout<<"姓名相同"<<endl;
		else cout<<"姓名不相同"<<endl;
	}

};
int main(){
   
student mate1("汪",01,100),mate2("徐",02,101);//构造两个student类对象 
mate1.write("大佬A",01,100);
mate2.write("大佬B",02,100);
mate1.showinfo();
mate2.showinfo(); 
mate1.compare(mate1,mate2); 
system("pause");
}

在student类中,使用了char *name来作为构造函数和write的形参,于是乎会发生E0167错误,按照其所说,我们将构造函数和write 的char *name 改为const char *name ,这时候会有第二个错误:

(2)E0513 不能将 “const char *” 类型的值分配到 “char *” 类型的实体
按照其所说,我们再将数据成员的char *name也改为const char *name,这时候可以运行

#include<iostream>
using namespace std;
class student
{
   
private:
	const char *name;
    long int num;
	int score;
		 
public:
	student(const char *Name=NULL ,long int Num=0 ,int Score =0 )   //带默认形参的构造函数
	{
   
		name=Name;
		num=Num;
		score=Score;
		cout<<"构造成功!"<<endl;
	}
	~student()                                               //析构函数
	{
   
		cout<<"析构成功!"<<endl;
	}
	student(student *p)                                     //拷贝构造函数
	{
   
		name=p->name;
		num=p->num;
		score=p->score;
		cout<<"拷贝构造成功!"<<endl;
	}
	void write(const char *Name,long int Num,int Score)          //设置学生信息
	{
   
		name=Name;
		num=Num;
		score=Score;
		cout<<"设置学生信息成功!"<<endl;
	}
	void showinfo()                                       //显示学生信息
	{
   
		cout<<name<<" "<<num<<" "<<score<<endl;
	}
	void compare(student a, student b)                   //比较学生信息是否相同 
	{
   
		if(a.num==b.num)
			cout<<"学号相同"<<endl;
		else cout<<"学号不相同"<<endl;
		if
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值