C++ 带字符串的AVL树错误:使用C+搜索/比较时访问冲突读取位置0x72746E69+;在visual studio 2015中

C++ 带字符串的AVL树错误:使用C+搜索/比较时访问冲突读取位置0x72746E69+;在visual studio 2015中,c++,avl-tree,C++,Avl Tree,这是我为一个班级写的一个程序。在这个程序中,我们将从20个不同的文件中读取数据,并将单个单词插入AVL树中。我们使用的算法(来自IndexType.cpp)是教授给我们的,我们被告知不要修改它们 我的问题是,当搜索(名为Find的函数)/插入(两者都发生)时,我得到错误访问冲突读取位置0x72746E69。读取第7个文件时,此错误始终出现在中间点附近 我尝试过绕过返回语句并完全重新构造代码,但似乎没有什么能解决问题。如果你能帮我弄明白这一点,我将不胜感激 有关守则如下: Main.cpp #in

这是我为一个班级写的一个程序。在这个程序中,我们将从20个不同的文件中读取数据,并将单个单词插入AVL树中。我们使用的算法(来自IndexType.cpp)是教授给我们的,我们被告知不要修改它们

我的问题是,当搜索(名为Find的函数)/插入(两者都发生)时,我得到错误访问冲突读取位置0x72746E69。读取第7个文件时,此错误始终出现在中间点附近

我尝试过绕过返回语句并完全重新构造代码,但似乎没有什么能解决问题。如果你能帮我弄明白这一点,我将不胜感激

有关守则如下:

Main.cpp

#include <fstream>
#include "indexType.h"
#include "occurrenceType.h"

using namespace std;

string filenameConverter(string& filename);

IndexType InputToIndex(IndexType& index);

int main()
{
    IndexType index;
    OccurrenceType idk;

    index = InputToIndex(index);

    index.InOrder(); //not relevant to my problem

    return 0;
}

IndexType InputToIndex(IndexType& index)
{
    int page;
    const int filemax = 21;

    ifstream* in;
    in = new ifstream[filemax];
    string filename;

    indexEntry entry;
    string word;

    occurrenceLocation save;

    page = 1;
    filename = "1.txt";
    in[page].open(filename);
    int position_counter = 0;

    while (page < 21)
    {
        while (in[page])
        {
            in[page] >> word;
            transform(word.begin(), word.end(), word.begin(), ::tolower);
            cout << word << " " << page << endl;
            position_counter++;

            index.Find(word);
            if (!index.IsPSet())
            {
                cout << "jep" << endl;
                entry.word = word;
                save.page = page;
                save.position = position_counter;
                entry.occurrences.Write(save);

                if (!index.IsFull())
                {
                        index.Insert(entry);
                        if (!index.IsPSet())
                        {
                            exit(1);
                        }
                }
            }
            else if (index.IsPSet())
            {
                save.page = page;
                save.position = position_counter;

                if (index.IsPSet())
                {
                    index.Write(save);
                }
            }
        }

        in[page].clear();

        if (filename == "20.txt")
        {
            return index;
        }
//filenameConverter changes the variable filename to the next file to read
        filename = filenameConverter(filename);

        position_counter = 0;
        page++;
        in[page].open(filename);
    }
    return index;
}
#包括
#包括“indexType.h”
#包括“发生类型.h”
使用名称空间std;
字符串文件名转换器(字符串和文件名);
索引类型输入索引(索引类型和索引);
int main()
{
索引类型索引;
发生型idk;
索引=输入索引(索引);
index.inoorder();//与我的问题无关
返回0;
}
索引类型输入索引(索引类型和索引)
{
整版;
常量int filemax=21;
ifstream*in;
in=新的ifstream[filemax];
字符串文件名;
指数输入法;
字符串字;
发生部位保存;
page=1;
filename=“1.txt”;
在[页面]中打开(文件名);
int位置_计数器=0;
而(第21页)
{
while(在[第页])
{
在[page]>>word中;
转换(word.begin(),word.end(),word.begin(),::tolower);
不正确);
}
}
void IndexType::Insert(indexEntry x、IndexNode*&q)
{
//假设q是通过引用传递的
//在调用Insert之前始终调用IsFull
//始终在调用Insert后调用IsPSet
//集p
if(q==NULL)
{
q=新索引节点;
q->entry.word=x.word;
q->left=NULL;
q->right=NULL;
p=q;
返回;
}
else if(x.word.compare(q->entry.word)<0)//错误发生的位置
{
插入(x,q->左);
如果(p!=NULL)
{
如果(高度(q->左)-高度(q->右)==2
{
如果(x.word.compare(q->left->entry.word)<0)
LL(q);
如果(x.word.compare(q->left->entry.word)>0,则为else
LR(q);
}
}
}
如果(x.word.compare(q->entry.word)>0),则为else
{
插入(x,q->右侧);
如果(p!=NULL)
{
如果(高度(q->右)-高度(q->左)=2
{
如果(x.word.compare(q->right->entry.word)>0)
RR(q);
否则如果(x.word.compare(q->right->entry.word)<0)
RL(q);
}
}
}
else if(x.word.compare(q->entry.word)==0)
{
出口(1);
p=NULL;//假定不允许重复
}
如果(p!=NULL)
{
q->高度=最大值(高度(q->左),高度(q->右))+1;
q->entry.executions.Write(x.executions.Read());
}
返回;
}

这是我第一次使用它来寻求帮助,因此如果您需要任何进一步的信息,请询问。感谢您的时间和帮助!

您必须使用调试器。
in=new-ifstream[filemax];
?您一次只读取一个流。您不需要一个流数组。您只是不喜欢第一个索引“0”吗?
page=1;
然后在[page]中忽略[0]中的
。如果然后使用索引
1+20=21
,则在[21]中使用
超出了您提供的范围。我之所以使用它,是因为我在[page]中遇到了问题循环。我已将其更改为在中使用,似乎我遇到的问题是在使用后没有关闭文件。感谢您的评论。我还使用了调试器,这是我如何知道问题所在的行,我只是不知道错误的确切含义或如何修复它。抱歉,不清楚。
void IndexType::Find(string x, IndexNode*& q)
{
    // assumes q is passed-by-value
    // sets p
    if (q == NULL)
    {
        p = q;
        return;
    }
    else if (q != NULL && x.compare(q->entry.word) == 0) //spot where the error occurs
    {
        p = q;
        return;
    }
    else if (x.compare(q->entry.word) < 0)
    {
        Find(x, q->left);
    }
    else if (x.compare(q->entry.word) > 0)
    {
        Find(x, q->right);
    }
}

void IndexType::Insert(indexEntry x, IndexNode*& q)
{
    // assumes q is passed-by-reference
    // always call IsFull prior to calling Insert
    // always call IsPSet after calling Insert
    // sets p
    if (q == NULL)
    {
        q = new IndexNode;
        q->entry.word = x.word;
        q->left = NULL;
        q->right = NULL;
        p = q;
        return;
    }
    else if (x.word.compare(q->entry.word) < 0) //spot where the error occurs
    {
        Insert(x, q->left);
        if (p != NULL)
        {
            if (Height(q->left) - Height(q->right) == 2)
            {
                if (x.word.compare(q->left->entry.word) < 0)
                    LL(q);
                else if(x.word.compare(q->left->entry.word) > 0)
                    LR(q);
            }
        }
    }
    else if (x.word.compare(q->entry.word) > 0)
    {
        Insert(x, q->right);
        if (p != NULL)
        {
            if (Height(q->right) - Height(q->left) == 2)
            {
                if (x.word.compare(q->right->entry.word) > 0)
                    RR(q);
                else if(x.word.compare(q->right->entry.word) < 0)
                    RL(q);
            }
        }
    }
    else if(x.word.compare(q->entry.word) == 0)
    {
        exit(1);
        p = NULL; // assumes duplicates are not allowed
    }
    if (p != NULL)
    {
        q->height = max(Height(q->left), Height(q->right)) + 1;
        q->entry.occurrences.Write(x.occurrences.Read());
    }
    return;
}