在2.exe中的0x00DE181818的未经手法异常:0xc0000005:访问违规写作位置0xcd7cbe10

Unhandled exception at 0x00de1818 in 2.exe: 0xC0000005: Access violation writing location 0xcd7cbe10

本文关键字:访问 0xc0000005 0xcd7cbe10 位置 异常 中的 exe 0x00DE181818      更新时间:2023-10-16

这是我的程序contact.h

#include <string>
#include <sstream>
#include <iostream>
using namespace std;
#pragma once
class Contact
{
private:
char *Name;
char *Last;
int Phone;
char *Address;

public:
    Contact operator +(Contact NewVal);
    Contact(char *newName,char *newLastName,char *newAddress,int newPhone);
    Contact(void);
    ~Contact(void);


void SetName(char *newName);
void SetLastName(char *newLastName);
void SetAddress(char *newAddress);
void SetPhone(int newPhone);

char* Contact::GetLast(void);
char* Contact::GetAddress(void);
int Contact::GetPhone(void);
char* GetName(void);
};

Contact.CPP

#include "StdAfx.h"
#include "Contact.h"

Contact::Contact(void)
{
}
Contact::Contact(char *newName,char *newLastName,char *newAddress,int newPhone)
{
    SetName(newName);
    SetLastName(newLastName);
    SetAddress(newAddress);
    SetPhone(newPhone);
}

Contact::~Contact(void)
{
}

void Contact::SetName(char *newName){
    Name=_strdup(newName);
}
void Contact::SetLastName(char *newLastName){
    Last=strdup(newLastName);
}
void Contact::SetAddress(char *newAddress){
    Address=strdup(newAddress);
}
void Contact::SetPhone(int newPhone){
    Phone=newPhone;
}
char* Contact::GetName(void)
{
    return Name;
}
char* Contact::GetLast(void)
{
    return Last;
}
char* Contact::GetAddress(void)
{
    return Address;
}
int Contact::GetPhone(void)
{
    return Phone;
}
Contact Contact::operator+(Contact NewVal)
{
            //strcat(this->Address,NewVal.Address);
            //strcat(this->Last,NewVal.Last);
            //strcat(this->Name,NewVal.Name);
            this->Phone=this->Phone+NewVal.Phone;
            sprintf(this->Address,"%s %s",this->Address,NewVal.Address);
            sprintf(this->Name,"%s %s",this->Name,NewVal.Name);
            sprintf(this->Last,"%s %s",this->Last,NewVal.Last);
            return *this;
}

phonebook.h

#include "Contact.h"
#pragma once
class PhoneBook
{
private:
        Contact member[100];
        int ID;
public:
    PhoneBook(void);
    ~PhoneBook(void);
    Contact Search(char* newName);
    bool AddNewContact(char* NewName, char* NewLast, char* NewAddress,int NewPhone);
    void ShowContacts(void);
};

phonebook.cpp

#include "StdAfx.h"
#include "PhoneBook.h"

PhoneBook::PhoneBook(void)
{
}

PhoneBook::~PhoneBook(void)
{
}

Contact PhoneBook::Search(char* newName)
{
    return Contact();
}

bool PhoneBook::AddNewContact(char* NewName, char* NewLast, char* NewAddress,int NewPhone)
{
    ID=ID+1;
    member[ID].SetName(NewName);
    member[ID].SetLastName(NewLast);
    member[ID].SetAddress(NewAddress);
    member[ID].SetPhone(NewPhone);
    return true;
}

void PhoneBook::ShowContacts(void)
{
    for(int a=0;a<=ID;a++){
        cout<<"Name:"<<member[ID].GetName()<<endl<<"LastName:"<<member[ID].GetLast()<<endl<<"Address:"<<member[ID].GetAddress()<<endl<<"Phone:"<<member[ID].GetPhone()<<endl;
    }
}

执行这些行后,我将获得访问违规错误(对于setName函数)

int _tmain(int argc, _TCHAR* argv[])
{
    PhoneBook mem;
    mem.AddNewContact("Bob","Jones","LA",100);

    return 0;
}

但是,此代码正常!!!这意味着在contact.h中设置函数。

int _tmain(int argc, _TCHAR* argv[])
{
    PhoneBook mem;
    Contact no("Bob","Jones","LA",100);
    //mem.AddNewContact("Bob","Jones","LA",100);

    return 0;
}

如果您能帮助我,我会很感激。

对不起我的错误。我忘记了为ID变量设置默认值。感谢史蒂夫和格雷的评论。