c++ - 错误 - _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

标签 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 个单词(名字和姓氏)写入变量“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/

相关文章:

c++ - 无依赖性最快的 C++ 信号/槽库

c++ - 在 Windows 中以编程方式区分 USB 软盘驱动器和 USB 闪存驱动器

c++ - 比较函数调用返回的字符串

c++ - 使用类模板的附加非类型模板参数嵌套调用可变参数方法

c++ - g++ 和 clang++ 具有完整模板参数的不同行为

c++ - openGL 3 问题 : undefined reference to _glapi_tls_Dispatch

javascript - 如何在 V8 Javascript 引擎中公开一个 C++ 类,以便可以使用 new 创建它?

c++ - 如何在不更改其余代码的情况下替换 C++ 模板以使其与 C 兼容?

c++ - WIN32 : How Do I Tell an Owner Drawn Static Control to Refresh Itself?

c++ - 将 cv::Mat 设置为其最大可能值