简体   繁体   中英

Error - _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

I have an error "_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)" and don't know, what should I do..

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;
}

Can you help me with this error? And also I'd like to know, how to write 2 words (name and surname) into variable "name" ? With "cin >> name" I can write only 1 word...

The destructor has to be defined as

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

And this statemenet

delete newperson;

has to be substituted for this statement

delete [] newperson;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM