为什么MyClass zhangsan(64,2000.00,"张三");64会报错
#pragma warning( disable : 4996)#include "iostream"
#include <string>
using namespace std;
class MyClass
{
int age;
float money;
char* name;
public:
MyClass();
MyClass(int age,float money,char* name);
MyClass(const MyClass &);
~MyClass();
char* getname();
float getmoney();
int getage();
void setmoney(float num);
void setage(int age);
private:
};
MyClass::MyClass()
{
age = 20;
money = 100000.00;
name = new char;
std:: strcpy(name,"无名氏");
}
MyClass::MyClass(int age1, float money1, char* name1)
{
age = age1;
money = money1;
name = new char;
std::strcpy(name, name1);
}
MyClass::MyClass(const MyClass &AnotherMyClass)
{
age = AnotherMyClass.age;
money = AnotherMyClass.money;
name = new char;
std::strcpy(name, AnotherMyClass.name);
}
MyClass::~MyClass( )
{
if (name != NULL) {
delete name;
}
}
char *MyClass::getname()
{
return name;
}
float MyClass::getmoney() {
return money;
}
int MyClass::getage() {
return age;
}
void MyClass::setage(int age1) {
if (age1 <=0) {
cout << "设置的年龄不合法" << endl;
}
else
{
age = age1;
}
}
void MyClass::setmoney(float num1) {
if (num1 < 0) {
cout << "设置的薪水不合法" << endl;
}
else
{
money = num1;
}
}
void main() {
MyClass zhangsan(64,2000.00,"张三");
cout << zhangsan.getage() << endl;
cout << zhangsan.getmoney() << endl;
cout << zhangsan.getname() << endl;
}
不应该是
#include<cstring>
或
#include<string.h>
吗{:10_277:} 这个应该跟你的标准选的有关, 我这边只需要把void main改为int main 然后return 0即可编译通过并且成功运行
页:
[1]