我有一个错误“_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)”,我不知道该怎么办..
person.h
#ifndef _person_H
#define _person_H
class person
{
private:
char * name;
int * age;
char * address;
public:
void get_info(void);
void show_info(void);
person();
~person();
};
#endif _person_H
person.cpp
#include "stdafx.h"
#include <stdlib.h>
#include <string>
#include "person.h"
#include <iostream>
using namespace std;
person::person()
{
this->name = new char[50];
this->age = new int;
this->address = new char[50];
}
person::~person()
{
delete this->name;
delete this->age;
delete this->address;
}
void person::get_info()
{
cout << "write name and surname:" << endl;
cin >> name;
cout << endl;
cout << "write age:" << endl;
cin >> *(age);
cout << endl;
cout << "write address:" << endl;
cin >> address;
cout << endl;
}
void person::show_info()
{
cout << "Name and surname:" << name << endl;
cout << "Age:" << *age << endl;
cout << "Address:" << address << endl;
}
main.cpp
#include "stdafx.h"
#include "person.h"
int _tmain(int argc, _TCHAR* argv[])
{
int i;
person * newperson = new person[5];
for (i = 0; i<5; i++){
newperson[i].get_info();
}
for (i = 0; i<5; i++){
newperson[i].show_info();
}
delete newperson;
return 0;
}
你能帮我解决这个错误吗?而且我还想知道,如何将 2 个单词(名字和姓氏)写入变量“name”?使用“cin >> name”我只能写一个字...
最佳答案
析构函数必须定义为
person::~person()
{
delete [] this->name;
delete this->age;
delete [] this->address;
}
还有这个声明
delete newperson;
必须替换此语句
delete [] newperson;
关于c++ - 错误 - _BLOCK_TYPE_IS_VALID(pHead->nBlockUse),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23594467/