#include<iostream.h>
#include<string>
#include<ctype.h>
class student
{
private:
int age;
int number;
char *name;
char sex;
public:
student(int i,int j,char *s,char c)
{
age=i;
number=j;
name=new char[strlen(s)+1];
strcpy(name,s);
sex=c;
}
student(const student &r)
{
age=r.age;
number=r.number ;
name=new char (*r.name );
sex=r.sex ;
}
~student();
void show_student();
};
student::~student()
{
delete []name;
};
void student::show_student()
{
cout<<age<<' '<<number<<' '<<name<<' '<<sex<<endl;
};
void main()
{
student B,A(21,3,"wang",'f');
A.show_student();
B=A;
B.show_student();
}
程序错在什么地方
编译显示:
--------------------Configuration: 1 - Win32 Debug--------------------
Compiling...
1.cpp
D:\vc\MSDev98\MyProjects\a\1.cpp(41) : error C2512: 'student' : no appropriate default constructor available
Error executing cl.exe.
1.exe - 1 error(s), 0 warning(s)
就是: student B,A(21,3,"wang",'f');有问题
參考答案:在申明student B时要用到默认构造函数,但你没有定义
加一个构造函数就可以了
student(){}