gpt4 book ai didi

c++ - 引发异常:读取访问冲突。这是对象数组中的nullptr

转载 作者:行者123 更新时间:2023-12-02 10:32:28 25 4
gpt4 key购买 nike

我是一名参加OOP类(class)的CS学生,但我不知道如何解决此问题。我知道,当+ =运算符尝试将第一个元素添加到数组中时,“this”为nullptr并引发异常,但我不知道如何解决它。

购物 list 标题看起来像这样:

#include "Groceries.h"

class ShoppingList{
Groceries* list;
int size = 0, capacity = 2;
public:
//methods
ShoppingList& operator+=( const Groceries& c);

运算符+ =看起来像:
ShoppingList& ShoppingList::operator+=( const Groceries& c) {
if (size == capacity) {
Groceries* l1 = new Groceries[capacity * 2];
l1 = list;
list = l1;
capacity *= 2;
}
list[size++]=c;//here is the exception
return *this;
}


杂货标题为:
#include <string>
#include <iostream>
class Groceries {
std::string product;
int quantity;
public:
Groceries() : product("empty"), quantity(0) {};
Groceries(std::string s, int x) : product(s), quantity(x) {};
Groceries(const Groceries& c);
~Groceries() {};
std::string product();
int quantity();
void Print();
};

和主要看起来像
int main()
{
ShoppingList L;
(L += Groceries("bread", 5)) += Groceries("cheese", 2);
L.Print();
//...
}

最佳答案

这些语句在运算符的主体中

l1 = list;
list = l1;

没有道理。在第一个赋值语句之后,由于已分配内存的地址丢失,因此发生内存泄漏。实际上,这两个语句与该语句等效
list = list;

包括覆盖指针 l1的副作用。

可以通过以下方式定义运算符
ShoppingList& ShoppingList::operator+=( const Groceries& c) {
if (size == capacity) {
Groceries* l1 = new Groceries[capacity * 2];
std::copy( list, list + size, l1 );
delete [] list;
list = l1;
capacity *= 2;
}
list[size++]=c;//here is the exception
return *this;
}

请注意,您正在使用相同的标识符 productquantity声明不同的实体
class Groceries {
std::string product;
int quantity;
public:
//...
std::string product();
int quantity();
//...

这是一个基于您的代码的演示程序。
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>

class Groceries {
std::string product;
int quantity;
public:
Groceries() : product("empty"), quantity(0) {};
Groceries(std::string s, int x) : product(s), quantity(x) {};
Groceries(const Groceries& c);
~Groceries() {};
// std::string product();
// int quantity();
void Print();

friend std::ostream & operator <<( std::ostream &os, const Groceries &g )
{
return os << g.product << ": " << g.quantity;
}
};

class ShoppingList{
Groceries* list;
int size = 0, capacity = 2;
public:
//methods
ShoppingList& operator+=( const Groceries& c);
ShoppingList() : list( new Groceries[2]() ) {}
~ShoppingList() { delete [] list; }

friend std::ostream & operator <<( std::ostream &os, const ShoppingList &sl )
{
std::copy( sl.list, sl.list + sl.size,
std::ostream_iterator<Groceries>( os, " " ) );
return os;
}
};

ShoppingList& ShoppingList::operator+=( const Groceries& c) {
if (size == capacity) {
Groceries* l1 = new Groceries[capacity * 2];
std::copy( list, list + size, l1 );
delete [] list;
list = l1;
capacity *= 2;
}
list[size++]=c;//here is the exception
return *this;
}


int main()
{
ShoppingList L;
(L += Groceries("bread", 5)) += Groceries("cheese", 2);

std::cout << L << '\n';

return 0;
}

程序输出为
bread: 5 cheese: 2 

关于c++ - 引发异常:读取访问冲突。这是对象数组中的nullptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61728465/

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