C++ 引发异常:读取访问冲突。这是对象数组中的null ptr
我是一名CS学生,正在学习OOP课程,我不知道如何解决这个问题。我知道当+=操作符尝试将第一个元素添加到数组中时,“this”是nullptr并引发异常,但我不知道如何修复它 购物列表标题如下所示:C++ 引发异常:读取访问冲突。这是对象数组中的null ptr,c++,class,oop,operator-overloading,compound-assignment,C++,Class,Oop,Operator Overloading,Compound Assignment,我是一名CS学生,正在学习OOP课程,我不知道如何解决这个问题。我知道当+=操作符尝试将第一个元素添加到数组中时,“this”是nullptr并引发异常,但我不知道如何修复它 购物列表标题如下所示: #include "Groceries.h" class ShoppingList{ Groceries* list; int size = 0, capacity = 2; public: //methods ShoppingList& operator+=(
#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();
};
杂货头看起来像:
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();
};
这些语句在运算符的正文中
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;
}
请注意,您使用相同的标识符产品
和数量
来声明不同的实体
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;
}
这些语句在运算符的正文中
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;
}
请注意,您使用相同的标识符产品
和数量
来声明不同的实体
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;
}
l1=列表;列表=l1代码>您希望这两行做什么?我希望列表中的元素存储在临时l1中,然后列表指向l1所在的内存。您用该语句泄漏了新的杂货店
。l1=list;列表=l1代码>您希望这两行做什么?我希望列表中的元素存储在临时l1中,然后列表指向l1所在的内存。您用该语句泄漏了新的杂货店
。例外仍然存在。它是在一个字符串函数中触发的,basic_string&assign(_in_reads_(_Count)const _Elem*const Ptr,_CRT_GUARDOVERFLOW const size_type _Count)@stack_仍然被损坏。您提供了一个甚至没有编译的代码段。我在回答中演示了如何更改运算符+=。您得到的错误是代码的其他缺陷造成的。例如,您可能忘记定义复制赋值运算符或其他内容。@stack\u仍然\u已损坏运算符+=定义的问题已解决。选择最佳答案关闭此问题,并询问一个新的问题,该问题描述了代码的新错误,提供了一个演示该问题的最小完整程序。异常仍然存在。它是在一个字符串函数中触发的,basic_string&assign(_in_reads_(_Count)const _Elem*const Ptr,_CRT_GUARDOVERFLOW const size_type _Count)@stack_仍然被损坏。您提供了一个甚至没有编译的代码段。我在回答中演示了如何更改运算符+=。您得到的错误是代码的其他缺陷造成的。例如,您可能忘记定义复制赋值运算符或其他内容。@stack\u仍然\u已损坏运算符+=定义的问题已解决。选择最佳答案关闭此问题,并询问一个新的问题,该问题描述了代码的新错误,提供了一个演示此问题的最小完整程序。