【问题标题】:Error - _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)错误 - _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
【发布时间】:2014-05-11 15:27:54
【问题描述】:

我有一个错误“_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;
}

ma​​in.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”我只能写1个字...

【问题讨论】:

标签: c++


【解决方案1】:

析构函数必须定义为

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

还有这个声明

delete newperson;

必须替换此语句

delete [] newperson;

【讨论】:

    猜你喜欢
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    • 2015-02-11
    • 2015-09-22
    • 2013-11-10
    • 1970-01-01
    相关资源
    最近更新 更多