一个简单的C++找错

来源:百度知道 编辑:UC知道 时间:2024/06/21 06:53:55
#include<iostream.h>
#include<string.h>
class Dog{
private:
char name[20];
int age;
char sex;
float weight;
public:
Dog(char *Name,int Age,char Sex,float Weight);
char *GetName(){return name;}
int GetAge(){return age;}
char GetSex(){return sex;}
float GetWeight(){return weight;}
void speak(){cout<<"Arf!Arf!"<<endl;}
};
Dog::Dog(char *Name,int Age,char Sex,float Weight){
strcpy(name,Name);
age=Age;
sex=Sex;
weight=Weight;
}
int main(){
Dog dog1("ahuang",2,"m",1.2);
cout<<"Dog'name:"<<dog1.GetName()<<endl;
cout<<"Dog'age:"<<dog1.GetAge()<<endl;
cout<<"Dog'Sex:"<<dog1.GetSex()<<endl;
cout<<"Dog'Weight:"<<dog1.GetWeight()<<endl;

你的构造函数的第三个参数定义的是字符,可是你在主函数中初始化
Dog类对象的时候赋值的是字符串,当然会报错了,
把Dog dog1("ahuang",2,"m",1.2);
改为:Dog dog1("ahuang",2,'m',1.2); 就OK了。