gpt4 book ai didi

C++ Int 似乎没有初始化并抛出异常 : read access violation

转载 作者:行者123 更新时间:2023-11-28 05:16:12 25 4
gpt4 key购买 nike

我是 C++ 的新手,所以请原谅草率的代码。这是有问题的代码:

包类

class Bag {
protected:
Item* _myItems;
int _numItems;
int _size;
public:
Bag();
Bag(int size);
~Bag();
Bag(Bag& original);
void add(Item a);
void remove(int itemnum);
int size();
int numItems();
void operator=(Bag& bag);
Item& operator[] (int i);
};

//Empty constructor
Bag::Bag() {
_numItems = 0;
}

//overloaded constructor
Bag::Bag(int size) {
_numItems = 0;
_myItems = new Item[size];
}

//copy constructor
Bag::Bag(Bag& original) {
//Copies the numItems
_numItems = original._numItems;
//Makes a new copy of the original array
_myItems = new Item[_numItems];
//Copies each element of the original into the new
for (int i = 0; i < _numItems; ++i) {
_myItems[i] = original[i];
}
}

//Destructor
Bag::~Bag(){
delete[] _myItems;
}

//Returns the size of the bag
int Bag::size()
{
return _size;
}

//Returns the number of items in the bag
int Bag::numItems() {
return _numItems;
}

//Add a new item to the bag
void Bag::add(Item a) {
int s = _numItems;
//Create a Item pointer and assign it to the array of the bag
Item* temp = _myItems;
//Assign _myItems to a new, larger array
_myItems = new Item[_numItems++];
//Copy the old array into the new one and nullify all the old array's items
for (int i = 0; i < _numItems - 1; i++) {
_myItems[i] = temp[i];
}
//Destroy the old array
delete[] temp;
//Add the item to the last position
_myItems[_numItems] = a;
}

我正在逐行读取一个文本文件。阅读似乎发生得很好。当我读入时,我执行这部分代码:

//The main program
int main() {

Pens * onePen = new Pens(1, 2);
Pens * twoPen = new Pens(2, 3);

Bag* bag = new Bag(5);

(*bag).add(onePen);
(*bag).add(twoPen);

bag[0];
bag[1];

int d = 0;

return 0;
}

当我进入添加方法时,我不断收到读取访问冲突(这是 0xc)。我还注意到,当我放置断点来检查代码时,_numItems 不是 0 而是 211。我是否以某种方式破坏了我的内存?

Here is a sample text file that we are using

Bag 和 Pen 类的简化版本(由 PaulMcKenzie 提供):

class Item {
protected:
int code_;

//Sets the method definition for the get/set methods and constructors
public:
Item(int code = -1);
virtual ~Item() {}
int getcode() const;
void setcode(int code);
std::ostream& operator<< (std::ostream& s);
bool operator== (const Item& a) const;
};

Item::Item(int code) : code_(code) {}
int Item::getcode() const { return code_; }
void Item::setcode(int code) { code_ = code; }

std::ostream & Item::operator<<(std::ostream& s)
{
s << " Code - " << code_ << "\n";
return s;
}

bool Item::operator==(const Item & a) const
{
return (code_ == a.getcode());
}

class Pens : public Item
{
private: int packetsize_;
public:
Pens();
Pens(int code, int packetsize);
int getpacketsize() const;
void setpacketsize(int packetsize);
bool operator== (const Pens& a) const;
};

Pens::Pens() :Item() { }
Pens::Pens(int code, int packetsize) : Item(code), packetsize_(packetsize) {}
int Pens::getpacketsize() const { return packetsize_; }
void Pens::setpacketsize(int packetsize) { packetsize_ = packetsize; }

std::ostream& operator<<(std::ostream& s, const Pens& pen)
{
s << " Packet size: " << pen.getpacketsize() << "\n";
return s;
}

bool Pens::operator==(const Pens & a) const
{
return code_ == a.getcode() && packetsize_ == a.getpacketsize();
}

最佳答案

我没有仔细看,但这段引起了我的注意:

//Add a new item to the bag
void Bag::add(Item a) {
int s = _numItems;
//Create a Item pointer and assign it to the array of the bag
Item* temp = _myItems;
//Assign _myItems to a new, larger array
_myItems = new Item[_numItems++];
//Copy the old array into the new one and nullify all the old array's items
for (int i = 0; i < _numItems - 1; i++) {
_myItems[i] = temp[i];
}
//Destroy the old array
delete[] temp;
//Add the item to the last position
_myItems[_numItems] = a;
}

请看这一行:

_myItems = new Item[_numItems++];

您创建大小为 _numItems 的新数组,然后_numItems 增加 1。

根据我的拙见,您的数组大小为 _numItems-1。然后你尝试使用元素 _myItems[_numItems] 所以这可能是内存损坏的原因。

关于C++ Int 似乎没有初始化并抛出异常 : read access violation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42591704/

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