簡體   English   中英

拋出異常:讀取訪問沖突。 這是對象數組中的 nullptr

[英]Exception thrown: read access violation. this was nullptr in array of objects

我是一名 CS 學生,正在學習 OOP 課程,但我不知道如何解決這個問題。 我知道當 += 運算符嘗試將第一個元素添加到數組中時,'this' 為 nullptr 並引發異常,但我不知道如何修復它。

購物清單 header 長這樣:

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

雜貨 header 看起來像:

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

沒有意義。 在第一個賦值語句之后,由於分配的 memory 的地址丟失,導致 memory 泄漏。 其實這兩個語句等價於這個語句

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

程序 output 是

bread: 5 cheese: 2 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM