出现类型转换错误 cannot convert parameter 1 from 'char' to 'char []' 10
求修改:#include<iostream>#include<assert.h>#include<string>usingnamespacestd;classPerson...
求修改:
#include<iostream>
#include<assert.h>
#include<string>
using namespace std;
class Person
{
private:
char name[20];
int age;
public:
Person(char *n="xxx",int a=50);
void setName(char n[20]);
void setAge(int a);
void output() const;
int input();
char getName();
};
Person::Person(char *n,int a)
{
strcpy(name,n);
age=a;
}
void Person::setName(char n[20])
{
int i;
i=strlen(n);//计数入i
assert(i<20);
strcpy(name,n);
}
void Person::setAge(int a)
{
age=a;
}
void Person::output() const
{
cout<<"姓名:"<<name<<"\t"<<"年龄:"<<age<<endl;
}
int Person::input()
{
cin.get(name,21,' ');//吞掉前20个字符
cin>>age;
if(name[20]=='\0')
{
return 0;
}
return 1;
}
char Person::getName()
{
return name[20];
}
int main(void)
{
const Person father("karl",55),mother("Brigitte",51);//调用函数
Person myself,another;
cout<<"father = ";
father.output();
cout<<"mother = ";
mother.output();
for (;;)
{
cin.clear();//是用来更改cin的状态标示符的。
cin.sync();//是用来清除缓存区的数据流的。
cout<<"please input name and age: ";
if (myself.input())
break;
cout<<"error!please try again...\n";
}
printf("myself = ");
myself.output();
Person son(father);
cout << "son = ";
son.output() ;
son.setName("Ludwig-Adalbert");
son.setAge(2);
cout << "son = ";
son.output() ;
another = mother;
cout << "another = ";
another.output();
// 不一定有意义,但是有可能的
son = son;
cout << "son = ";
son.output() ;
// 不一定有意义,但是有可能的
son.setName(son.getName());
cout << "son = ";
son.output() ;
return 0 ;
} 展开
#include<iostream>
#include<assert.h>
#include<string>
using namespace std;
class Person
{
private:
char name[20];
int age;
public:
Person(char *n="xxx",int a=50);
void setName(char n[20]);
void setAge(int a);
void output() const;
int input();
char getName();
};
Person::Person(char *n,int a)
{
strcpy(name,n);
age=a;
}
void Person::setName(char n[20])
{
int i;
i=strlen(n);//计数入i
assert(i<20);
strcpy(name,n);
}
void Person::setAge(int a)
{
age=a;
}
void Person::output() const
{
cout<<"姓名:"<<name<<"\t"<<"年龄:"<<age<<endl;
}
int Person::input()
{
cin.get(name,21,' ');//吞掉前20个字符
cin>>age;
if(name[20]=='\0')
{
return 0;
}
return 1;
}
char Person::getName()
{
return name[20];
}
int main(void)
{
const Person father("karl",55),mother("Brigitte",51);//调用函数
Person myself,another;
cout<<"father = ";
father.output();
cout<<"mother = ";
mother.output();
for (;;)
{
cin.clear();//是用来更改cin的状态标示符的。
cin.sync();//是用来清除缓存区的数据流的。
cout<<"please input name and age: ";
if (myself.input())
break;
cout<<"error!please try again...\n";
}
printf("myself = ");
myself.output();
Person son(father);
cout << "son = ";
son.output() ;
son.setName("Ludwig-Adalbert");
son.setAge(2);
cout << "son = ";
son.output() ;
another = mother;
cout << "another = ";
another.output();
// 不一定有意义,但是有可能的
son = son;
cout << "son = ";
son.output() ;
// 不一定有意义,但是有可能的
son.setName(son.getName());
cout << "son = ";
son.output() ;
return 0 ;
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询