Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 0xC0000005:访问冲突读取位置0x00000000。c++;vs2015_C++_Visual Studio 2015 - Fatal编程技术网

C++ 0xC0000005:访问冲突读取位置0x00000000。c++;vs2015

C++ 0xC0000005:访问冲突读取位置0x00000000。c++;vs2015,c++,visual-studio-2015,C++,Visual Studio 2015,我正在使用一个简单的文本解析器创建一个RPG,并且在文本解析器中仍然存在访问写入冲突的问题。以下是我目前的代码: /* Author: Michael Norris Program: Generiquest Filename: TextParser.cpp Maintainence Log: 10/28/2015 Created textParser.cpp Notes: This text parser is fairly inefficient, but is easier to man

我正在使用一个简单的文本解析器创建一个RPG,并且在文本解析器中仍然存在访问写入冲突的问题。以下是我目前的代码:

/*
Author: Michael Norris
Program: Generiquest
Filename: TextParser.cpp
Maintainence Log:
10/28/2015 Created textParser.cpp


Notes:
This text parser is fairly inefficient, but is easier to manage and understand for beginners.

*/



#include <conio.h>
#include <stdio.h>
#include <Windows.h>
#include <time.h>
#include "myheader.h"
#include <iostream>
#include <string.h>


using namespace std;

class Words
{
public:
Words()
{
    word verbs[30];//This is an array of all the verbs
    strcpy(verbs[0].text, "Items"); 
    strcpy(verbs[1].text, "Stats");
    strcpy(verbs[2].text, "Use");
    strcpy(verbs[3].text, "Eat");
    strcpy(verbs[4].text, "Throw");
    strcpy(verbs[5].text, "Drop");
    strcpy(verbs[6].text, "Look");
    strcpy(verbs[7].text, "Move");
    strcpy(verbs[8].text, "Put");
    strcpy(verbs[9].text, "Speak");
    strcpy(verbs[10].text, "Attack");
    strcpy(verbs[11].text, "Go");
    strcpy(verbs[12].text, "Climb");
    strcpy(verbs[13].text, "Open");
    strcpy(verbs[14].text, "Take");
    strcpy(verbs[15].text, "Put");
    strcpy(verbs[16].text, "Kill");
    strcpy(verbs[17].text, "Get");
    strcpy(verbs[18].text, "LOL");

    //End of verb declarations
    for (int ele = 0; ele < 19; ele++)
    {
        verbs[ele].type = verb;
    }

}




};

Words mainWords;
void textParser()
{


    char str[51] = "Test String";
    char test[50] = "";
    char word1[20] = "";
    //char * word2;
    char word3[20] = "";
    char word4[20] = "";
    system("cls");
    scanf("%50[0-9a-zA-Z ]", &test);
    flushall();
    strcpy(word3, strtok(test, " ,.-"));
    int cray;
    for (bool correctI = false; correctI == false;)
    {

        if (word3 != NULL)
        {
            strcpy(word1, strtok(NULL, " ,.-"));
            cray = strcmp(word1, NULL);//Error thrown here
            if (cray != 0)
            {
                strcpy(word4, word1);

            }
        }
        printf("%s", word3);
        printf("%s", word4);
        cray = stricmp(word1, "Items");
        if (cray = 0)
        {
            printf("Success!!");

        }
        else
        {
            printf("Fail");
        }
    }
    _getch();


}

//TODO: use stricmp()
/*
作者:迈克尔·诺里斯
程序:Generiquest
文件名:TextParser.cpp
维护日志:
2015年10月28日创建textParser.cpp
笔记:
这个文本解析器效率很低,但对于初学者来说更容易管理和理解。
*/
#包括
#包括
#包括
#包括
#包括“myheader.h”
#包括
#包括
使用名称空间std;
课堂用语
{
公众:
字()
{
单词动词[30];//这是所有动词的数组
strcpy(动词[0]。文本,“项”);
strcpy(动词[1]。文本,“Stats”);
strcpy(动词[2]。文本,“使用”);
strcpy(动词[3]。文本“Eat”);
strcpy(动词[4]。文本“Throw”);
strcpy(动词[5]。文本“Drop”);
strcpy(动词[6]。文本,“Look”);
strcpy(动词[7]。文本,“移动”);
strcpy(动词[8]。文本,“Put”);
strcpy(动词[9]。文本,“说话”);
strcpy(动词[10]。文本,“攻击”);
strcpy(动词[11]。文本“Go”);
strcpy(动词[12]。文本“爬升”);
strcpy(动词[13]。文本,“打开”);
strcpy(动词[14]。文本,“Take”);
strcpy(动词[15]。文本,“Put”);
strcpy(动词[16]。文本,“Kill”);
strcpy(动词[17]。文本“Get”);
strcpy(动词[18]。文本,“LOL”);
//动词结尾声明
对于(int ele=0;ele<19;ele++)
{
动词[ele]。类型=动词;
}
}
};
关键词;
void textParser()
{
char str[51]=“测试字符串”;
字符测试[50]=“”;
char word1[20]=“”;
//char*word2;
char word3[20]=“”;
char word4[20]=“”;
系统(“cls”);
scanf(“%50[0-9a-zA-Z]”,&测试);
flushall();
strcpy(字3,strtok(test),.-”);
内特克雷;
for(bool correctI=false;correctI==false;)
{
if(word3!=NULL)
{
strcpy(字1,strtok(空,,.-”);
cray=strcmp(word1,NULL);//此处抛出错误
如果(克雷!=0)
{
strcpy(word4,word1);
}
}
printf(“%s”,字3);
printf(“%s”,字4);
cray=stricmp(字1,“项目”);
如果(cray=0)
{
printf(“成功!!”);
}
其他的
{
printf(“失败”);
}
}
_getch();
}
//TODO:使用stricmp()
我在文本解析器函数中遇到了麻烦

void textParser()
{

}

仍然在上抛出accces冲突错误 strcpy(字1,strtok(空,,.-”)

对你(以前)的TL的一些评论;柯德博士名单


备注:请勿使用flushall() 看见 相反,只需要一个读取缓冲区的循环,例如使用fgetc() 或者使用另一种方法-见下一条备注

备注:要更好地控制输入,请使用FGET和 然后sscanf,把它放在一个单独的函数中


备注:这是不寻常的,
do{…},而(!correctI)
会更清楚


备注:要检查word1是否为空,请改为

char* tok = strtok(NULL, " ,.-");
if (tok != NULL)
{        
  strcpy(word4,tok);
}

备注:可以写成

word verbs[] = { {"Items", 0}, {"Stats", 1}, ... };    

<>你用类写C,不是C++ +< /p> C++通过在指针问题之上构建精简的抽象来避免许多指针问题

例如,您可以使用
std::string
,而不是使用
char*
C样式的数组。所以与其说不安全

const char* word1 = "Whatever";
cray = stricmp(word1, "Items");
    if (cray == 0) {
你会得到

std::string word1 = "Whatever";
    if("Items" == word1) {
没有类似的

strcmp(word1, NULL)
因为比较字符串和空指针是没有意义的(在C中是不允许的)

您可能希望与空字符串进行比较:使用literal
“”

请注意,如果(cray=0)(与上面的代码相比),则在
中也有一个错误

此外,无论何时出现bug,都不应立即在最近的论坛或StackOverflow上发布代码。相反,您需要在调试器下启动应用程序,并尝试自己找出问题所在。这样您将更好地理解代码

<>我想在尝试用C++编写更多的C++和标记更多的问题之前,你应该选一些关于这个主题的好书。这将对你们两人以及整个社区都更有成效。因此,在这方面有一个很棒的帖子:


在此处发布相关代码,明确指出调试器所说的导致错误的代码行,以及相关变量的值(如果已知)。在您这边做一些初步调查,并发布一段有问题的代码片段。添加的链接有100行代码。哪一行代码会导致访问冲突?第一次strcmp调用您给strcmp的是NULL,并怀疑您是否会得到NULL引用错误?
word verbs[30];//This is an array of all the verbs
strcpy(verbs[0].text, "Items"); 
strcpy(verbs[1].text, "Stats"); 
...
word verbs[] = { {"Items", 0}, {"Stats", 1}, ... };    
const char* word1 = "Whatever";
cray = stricmp(word1, "Items");
    if (cray == 0) {
std::string word1 = "Whatever";
    if("Items" == word1) {
strcmp(word1, NULL)