我在C++函數應該成員添加到一個鏈表,但是當它執行它拋出一個異常:訪問衝突讀取位置0x0000001
Unhandled exception at 0x5b9414cf (msvcr100d.dll) in Sample.exe: 0xC0000005: Access violation reading location 0x00000001
這發生時,一些變量不明確...但我無法弄清楚這個變量在哪裏......也有斷點。 這裏整個代碼(除.h文件...但它僅包含聲明)
#include "stdafx.h"
#include "winsock.h"
#include "windows.h"
#include <string.h>
#include <iostream>
#include "Sample.h"
using namespace std;
struct BUNNY
{
public:
int sex;
int name;
int age;
bool colour;
int radioactive_mutant_vampire_bunny;
BUNNY *nextBunny;
};
int returnSex();
int returnName (int sex);
int returnColour();
bool radioactiveBunny();
string translateName(int name);
string translateSex(int sex);
string translateColour(int colour);
BUNNY * AddBunny(BUNNY * head,int sex,int name,int colour,bool radioactive_mutant_vampire_bunny);
BUNNY * travereseBunny(BUNNY * head);
BUNNY * displayBunny();
int _tmain(int argc, _TCHAR* argv[])
{
bool flag = true;
int turn = 0;
BUNNY * head = new BUNNY;
head = NULL;
while (flag)
{
++turn;
if (turn == 1)
{
for (int i = 1; i <= 5; i++)
{
int sex = returnSex();
int name = returnName(sex);
int colour = returnColour();
bool radioactive = radioactiveBunny();
head = AddBunny(head,sex,name,colour,radioactive);
printf("A new bunny is born: sex %s , name %s , colour %s , radioactive %d",head->sex,head->name,head->colour,head->radioactive_mutant_vampire_bunny);
system("pause");
}
}
}
return 0;
}
int returnSex()
{
int random = rand() % 100 + 1;
if(random > 50)
return MALE;
else
return FEMALE;
}
int returnName(int sex)
{
if (sex == MALE)
{
int random = (rand() % 100 + 1)/5;
if(random < 20)
return BU;
else if (random > 20 && random <40)
return CRYSTAL;
else if(random > 40 && random < 60)
return JASON;
else if(random > 60 && random < 80)
return ERO;
else if(random > 80)
return METH;
}
else
{
int random1 = rand() % 100 + 1;
if (random1 < 50)
{
return MARIA;
}
else if (random1 > 50)
return JAMIE;
}
}
int returnColour()
{
int random = rand() % 4 + 1;
if(random == 1)
return WHITE;
else if(random > 1 && random <= 2)
return BROWN;
else if(random > 2 && random <= 3)
return BLACK;
else if(random > 3 && random <= 4)
return SPOTTED;
}
bool radioactiveBunny()
{
int random = rand() % 100 + 1;
if(random > 0 && random <= 2)
return true;
else
return false;
}
BUNNY * AddBunny(BUNNY * head,int sex,int name,int colour,bool radioactive_mutant_vampire_bunny)
{
BUNNY * newBunny = new BUNNY;
newBunny->age = 0;
newBunny->colour = colour;
newBunny->sex = sex;
newBunny->name = name;
newBunny->radioactive_mutant_vampire_bunny = radioactive_mutant_vampire_bunny;
newBunny->nextBunny = head;
return newBunny;
}
string translateName (int name)
{
switch (name)
{
case CRYSTAL: return "CRYSTAL";
break;
case BU: return "BU";
break;
case JASON: return "JASON";
break;
case ERO: return "ERO";
break;
case METH: return "METH";
break;
case MARIA: return "MARIA";
break;
case JAMIE: return "JAMIE";
break;
}
}
string translateColour (int colour)
{
switch (colour)
{
case WHITE: return "WHITE";
break;
case BLACK: return "BLACK";
break;
case BROWN: return "BROWN";
break;
case SPOTTED: return "SPOTTED";
break;
}
}
string translateSex (int sex)
{
switch (sex)
{
case MALE: return "MALE";
break;
case FEMALE: return "FEMALE";
break;
}
}
這是整個代碼...希望這將有助於。 感謝您的時間:)
怎麼樣給我們提供線路? – Paranaix
你能給我們看看BUNNY嗎? –
我幾乎可以肯定,這個錯誤不在你提供的代碼片段中,但是我們必須看到這個'BUNNY'的聲明。 – pasztorpisti