错误 - _BLOCK_TYPE_IS_VALID(pHead-> nBlockUse)

时间:2014-05-11 15:27:55

标签: c++

我有一个错误" _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个单词(姓名和姓氏)写入变量&#34; name&#34; ?使用&#34; cin&gt;&gt;命名&#34;我只能写一个字......

1 个答案:

答案 0 :(得分:1)

析构函数必须定义为

person::~person()
{
    delete [] this->name;
    delete this->age;
    delete [] this->address;
}

这句话

delete newperson;

必须替换此声明

delete [] newperson;