2.exe中0x00de1818处的未处理异常:0xC0000005:访问冲突写入位置0xcd7cbe10

时间:2017-09-19 16:38:31

标签: c++

这是我的计划 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;
}

如果你能帮助我,我将不胜感激。

1 个答案:

答案 0 :(得分:-1)

抱歉我的错误。我忘了为ID变量设置默认值。 感谢Steve和Gray的评论。