【问题标题】:First-chance exception at 0x00998876 in Project4.exe: 0xC0000005: Access violation writing location 0x00000000Project4.exe 中 0x00998876 处的第一次机会异常:0xC0000005:访问冲突写入位置 0x00000000
【发布时间】:2013-12-03 17:08:45
【问题描述】:

我正在写一个刽子手游戏。我无法继续调试,因为我不明白这个错误:

Project4.exe 中 0x00998876 处的第一次机会异常:0xC0000005: 访问冲突写入位置 0x00000000。未处理的异常 Project4.exe中的0x00998876:0xC0000005:访问冲突写入 位置 0x00000000。

这是产生错误的地方:

void Player::getWords()
{ 
  ifstream WordBank;
  int index=0;
  WordBank.open("C:\\WordBank\\words.txt");

  if(WordBank)
  {
      for(index=0;index<100;index++)
      {     
           WordBank>>Words1[index];     
      }
      WordBank.close();
  }
  else
  {
      cout<<"There was an error."<<endl;
  } 
}

Words 数组被声明为成员变量。

这是我的代码。我还不确定如何格式化单词,我正在尝试完成这个程序。

class Player
{   public:
    string fName;
     string lName;
     int DOB;
     string username;
     int SS4;
     string email;
     int won;
     int lost;
     const int static  WordSIZE=15;
     int const static totalWORDS=100;
     string static Letters[WordSIZE];
     string static  Words1[totalWORDS];
     char static   Copy[WordSIZE];
     char static Guess[WordSIZE];
     int index;
     int word;
     int size;
     int isComing;//I need function to initialize these.
     char letter;
     bool correct;//I need a function to initialize these.
     string Word1;
public:
    Player(string,string,int,string,int,string);
    void getWords();
    void boardSetup();
    void playGame();
    void deathBed(int);



};

Player::Player(string first,string last,int birth, string nicname,int SS,string mail)
{
 fName=first;
 lName=last;
 DOB=birth;
 username=nicname;
 SS4=SS;
 email=mail;
 isComing=0;
 correct=true;
}
    const int static  WordSIZE=15;
     int const static totalWORDS=100;
  string Player::  Words1[totalWORDS]; 
    char Player::   Copy[WordSIZE];
     char Player:: Guess[WordSIZE];
      string Player:: Letters[WordSIZE];
 void Player::getWords()
{ 





  ifstream WordBank;
  int index=0;
    WordBank.open("C:\\WordBank\\words.txt");


    if(WordBank)
    {
         while(WordBank>>Words1[index])
         {
           index++;
         }
     WordBank.close();
    }
    else
        {
          cout<<"There was an error."<<endl;
        }

}


    /*string *words2;
    words2=new  string[100];
  ifstream WordBank;
  int index;
    WordBank.open("C:\\WordBank\\words.txt");


    if(WordBank)
    {
       for(index=0;(WordBank>>words2[index]);index++)
        {



        }
    WordBank.close();
    }
    else
        {
          cout<<"There was an error."<<endl;
        }
    delete [] words2;
   words2=0;
}*/

void Player::boardSetup()
{
    unsigned seed =time(0);
    srand(seed);
    word=rand()%100;
    Words1[word]=Word1;
    strcpy_s(Copy,Word1.c_str());

    size=strlen(Word1.c_str());
    for(index=0;index<size;index++)
    {
        Guess[index]='-';
        cout<<Guess[index]<<endl;
    }


}







}

void Player::playGame()
{
    while(isComing!=7)
    {
        deathBed(isComing);
        cout<<Guess<<endl;
        cout<< "Please guess a letter."<<endl;// or press 0 to go to the main screen for help
        cin>>letter;
        letter=toupper(letter);

            for (index=0;index<size;index++)
        {
            if(Copy[index]==letter)
            {
               cout<<"Nice Job"<<endl; //add the ability to see the word
               Guess[index]=letter;
               cout<<Guess[index]<<endl;

            }
            else  if(strcmp(Word1.c_str(),Guess)==0)
              {
                  cout<<"You WIN!!!"<<endl;
                  return;
              }

            else if (correct=false)
            {
                cout<<"Please,Try again"<<endl;
                isComing++;
            }
        }

    }
     void deathBed(int isComing);

     cout<<"The word is"<<Words1[word]<<"."<<endl;

     //draw a big red noose. call a function for it.

}


struct userInfo
{
    string FName;
    string LName;
    int dob;
    string Username;
    int ss4;
    string Email;
};

userInfo getUserInfo();


int main()
{ 
  userInfo i; 
     i=getUserInfo();
 Player player1(i.FName,i.LName,i.dob,i.Username,i.ss4,i.Email);
 player1.getWords();
 player1.boardSetup();
 player1.playGame();

    return 0;
}

userInfo getUserInfo()
{
    userInfo info;

    cout<<"What is your first name?"<<endl;
    cin>> info.FName;
    cout<<"What is your last name?"<<endl;
    cin>>info.LName;
    cout<<"What is your date of birth"<<endl;
    cin>>info.dob;
    cout<<"Please enter a user name."<<endl;
    cin>>info.Username;
    cout<<"Please enter the last four digits of your Social Security number."<<endl;
    cin>>info.ss4;
    cout<<"Please enter your email address."<<endl;
    cin>>info.Email;

    return info;
}

【问题讨论】:

  • 错误告诉你,你正在尝试写入一个为 NULL 的指针。
  • 你能出示 Words1 的声明吗?
  • 你的调试器没有准确地告诉你哪一行正在通过空指针写入吗?
  • 或者 Player 本身是否为 NULL?
  • 他正在查看输出窗口而不是调试器。

标签: c++


【解决方案1】:

您没有向我们展示您的测试用例,但调试错误清楚地表明您正在通过空指针(可能位于Words1)进行写入。

【讨论】:

  • 感谢您的快速回复。我没有使用任何指针。我知道数组是指向内存地址的常量指针,但是我没有声明任何动态内存分配的指针。(什么是测试用例?我正在查看输出窗口。我只编程了大约 3 个月。我我不确定如何阅读或调试器窗口是什么。)我确实研究了这个问题,大多数人说我试图取消引用一个空指针。这意味着我正在使用一个指向“null”或什么都没有的指针。
  • 字符串静态 Words1[totalWORDS];
  • 那是我的班级声明或 Words1[totalWords]。
  • 我知道你们很忙,至少能给我指明一个好的方向。
  • 没有您的帮助,我们真的无法提供更多帮助。您需要发布更多代码,因为我们必须猜测您发布的内容。请编辑您的原始问题。
【解决方案2】:

我在 Visual Studio 2012 上调试了您的代码。您的 boardSetup() 函数存在问题,导致无法玩游戏。您对 Word1 的分配是错误的方式。我的意思是你应该有

Word1=Words1[word];

代替

Words1[word]=Word1;

在此之后程序运行,你可以玩了。然而,当你赢了它并没有这么说。它只是不断要求更多的信件。我会把这个练习留给你。我没有看到任何崩溃、空指针访问或 bad_alloc(尽管这些都不是预期的,因为您没有使用指针)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    相关资源
    最近更新 更多