1

我是一名正在学习 OOP 课程的 CS 学生,但我不知道如何解决这个问题。我知道当 += 运算符尝试将第一个元素添加到数组中时,'this' 为 nullptr 并引发异常,但我不知道如何修复它。

购物清单标题如下所示:

#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();
//...
}
4

1 回答 1

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;
}

请注意您使用相同的标识符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 
于 2020-05-11T11:26:16.537 回答