gpt4 book ai didi

c++ - 整数在 Visual Studio 2012 c++ 中更改其自身的值

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:28:30 25 4
gpt4 key购买 nike

<分区>

此类的大小变量在构造函数中设置为零。只有当你向数组中添加一个项目或删除一个项目时,它才会增加或减少,在这种情况下,将其切成两半,大小将减少到容量的 1/2。

但是,我的size变量好像跑掉了,随机变成了随机数,比如516846,不在它设置的size范围内。我检查并跟踪我的程序,找不到任何改变大小的东西,我知道大小和容量是在构建时设置的。

#pragma once

#include <string>
#include <iostream>
using namespace std;

template <typename ItemType>
class Node
{
private:
ItemType* items;
int size;
int capacity;

public:
Node* nextNode;
Node* prevNode;

Node(Node* pNode, Node* nNode, int limit)
{
prevNode = pNode;
nextNode = nNode;
capacity = limit;
size = 0;

if(capacity != 0)
items = new ItemType[capacity];

cout << "Node() capcity = " << capacity << " size = " << size << endl;
};

~Node(void){};

int getSize()
{
return size;
};

void addItem(int index, ItemType item)
{
cout << "node->addItem" << endl;
for(int i = (getSize() - 1); i >= index; i--)
{
items[i + 1] = items[i];
}
items[index] = item;
size ++;
};
void addItem(ItemType item)
{
cout << "node->addItem" << endl;
items[getSize()] = item;
size ++;
};

void deleteItem(int index)
{
cout << "node->deleteItem index = " << index << endl;

for(int i = index; i < (getSize() - 1); i++)
{
items[i] = items[i+1];
}
size --;
};


void cleaveInHalf()
{
cout << "node->cleaveInHalf" << endl;
size = capacity/2;
};

bool isFull()
{
return ((getSize() >= capacity) ? true : false);
};

};

当调用 isFull() 函数时,我收到错误“访问冲突读取位置 0x00000004”。大小是一些奇怪的数字,例如 51515615。

    void insert(int index, const ItemType& item) 
{
cout << "lal->insert" << endl;
if (index > size)
return;

//if we have no nodes to hold data make a new one between head and tail
if (head->nextNode == tail)
{
linkNewNode(head, tail);
}
// lets find the node to put it in and the spot in the array of the node
int indexIntoArray = 0;
Node<ItemType>* temp = getNodeContainingIndex(index, indexIntoArray);
if(temp->isFull())
{
// if we are full then we ant to split, and then call this function again to find the new location to go in.
splitNode(temp);
insert(index, item);
return;
}
// and now insert it in
cout << "lal->----attempting to add item at " << indexIntoArray << endl;
temp->addItem(indexIntoArray, item);
size ++;
};

Node<ItemType>* getNodeContainingIndex(int index, int& indexIntoArray)
{
cout << "lal->getNodeContainingIndex" << endl;
Node<ItemType>* temp;
if (index == size)
{
temp = tail->prevNode;
indexIntoArray = temp->getSize();
}
else if (index <= (size/2)) /* coming from 0*/
{
cout << "lal->----coming from 0" << endl;
int position = 0;
temp = head->nextNode;
position = position + temp->getSize();

while (position < index)
{
temp = temp->nextNode;
position = position + temp->getSize();
}
indexIntoArray = index - (position - temp->getSize());
return temp;
}
else /*coming from size*/
{
cout << "lal->----coming from size = " << size << endl;
int position = size;
temp = tail;
while (position > index)
{
temp = temp->prevNode;
position = position - temp->getSize();
}
indexIntoArray = abs(position - index);
return temp;
}
}

Node<ItemType>* linkNewNode(Node<ItemType>* prev, Node<ItemType>* next)
{
cout << "lal->linkNewNode" << endl;
Node<ItemType>* temp = new Node<ItemType>(prev, next, arrayCapacity);
prev->nextNode = temp; next->prevNode = temp;
numOfNodes ++;
return temp;
}

它在 getNodeContainingIndex 函数中中断,就在这里

int indexIntoArray = 0;
Node<ItemType>* temp = getNodeContainingIndex(index, indexIntoArray);
if(temp->isFull())
{

在 temp->isFull() 行。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com