2016-05-14 66 views
1

我有作業使用動態分配的內存。我的教授給了我一些指示。使用它們,我編碼了下面的代碼。隨機時間出現錯誤。在執行復制之前有時出現錯誤。有時它會複製一個對象而不會複製下一個對象。我不明白我做錯了什麼。動態分配內存,複製構造函數中的錯誤

默認的構造

GroceryItem::GroceryItem() 
{ 
    item_name = new char[strlen("") + 1]; 
    strcpy(item_name, ""); 
    item_price = 0; 
    qty_on_hand = 0; 
    qty_purchased = 0; 
}; 

功能下面是我用它來複制兩個對象的拷貝構造函數:

GroceryItem::GroceryItem(const GroceryItem& Grocery_in) 
{ 
    item_name = new char[strlen(Grocery_in.item_name) + 1]; 
    strcpy(item_name, Grocery_in.item_name); 
    item_price = Grocery_in.item_price; 
    qty_on_hand = Grocery_in.qty_on_hand; 
    qty_purchased = Grocery_in.qty_purchased; 
} 
; 
下面

被分配新建分配FY opperator

GroceryItem& GroceryItem::operator=(GroceryItem& copy_item) 
{ 
    if (this == &copy_item) 
     return *this; 
    else 
    { 
     delete[] item_name; 
     item_name = new char[strlen(copy_item.item_name)+1]; 
     strcpy(item_name, copy_item.item_name); 
     item_price = copy_item.item_price; 
     qty_on_hand = copy_item.qty_on_hand; 
     qty_purchased = copy_item.qty_purchased; 
     return *this ;  // They are the same 
    } 
} 

從函數調用如下當我嘗試複製到temp2:

void sort_items(GroceryItem ini_customer_GroceryItem[], int number) 
{ 
    int j = 0, k = 0; 
    GroceryItem temp2; 

    for (j = 0; j < number - 1; j++) // n-1 passes 
    { 
     for (k = number - 1; j < k; k--) // each pass runs one fewer than the preceding one 
     { 
      if (ini_customer_GroceryItem[k - 1] > ini_customer_GroceryItem[k]) 
      { 
       temp2 = ini_customer_GroceryItem[k - 1]; 
       ini_customer_GroceryItem[k - 1] = ini_customer_GroceryItem[k]; 
       ini_customer_GroceryItem[k] = temp2; 
      } 
     } 
    } 
} 

以下是錯誤

image

+1

你的教授是在幫你幫倒忙通過指示你寫陳舊的C代碼strcpy()和strlen(),而不是花時間教你真正的C++代碼,比如'std :: string',它不需要任何這些廢話。爲了成爲一名熟練的C++開發人員,您沒有被教授必要的技能。附:我看到複製構造函數沒有錯。內存損壞很可能發生在其他地方。僅僅因爲你的代碼在一個特定的地方崩潰了,也就是拷貝構造函數,並不意味着它就是錯誤所在。請發佈[mcve]。 –

+0

這不是一個拷貝構造函數,它是一個賦值操作符,它應該通過const引用來引用它的參數。「 –

+0

」下面的函數是拷貝構造函數「 - 實際上它是賦值操作符。你的拷貝構造函數在哪裏? –

回答

1

sort_items()功能應該使用std::swap()算法,而不是複製手動對象:

/* 
temp2 = ini_customer_GroceryItem[k - 1]; 
ini_customer_GroceryItem[k - 1] = ini_customer_GroceryItem[k]; 
ini_customer_GroceryItem[k] = temp2; 
*/ 
std::swap(ini_customer_GroceryItem[k - 1], ini_customer_GroceryItem[k]); 

無論哪種方式,你沒有實現副本構造函數,只有一個複製賦值運算符(和copy_item在您的實施中應爲const)。見Rule of Three。您需要實現合適的拷貝構造函數:

GroceryItem::GroceryItem(const GroceryItem& source_item) 
{ 
    item_name = new char[strlen(source_item.item_name)+1]; 
    strcpy(item_name, source_item.item_name); 
    item_price = source_item.item_price; 
    qty_on_hand = source_item.qty_on_hand; 
    qty_purchased = source_item.qty_purchased; 
} 

然後你就可以使用拷貝構造函數實現你的拷貝賦值運算符:

GroceryItem& GroceryItem::operator=(const GroceryItem& copy_item) 
{ 
    if (this != &copy_item) 
    { 
     GroceryItem temp(copy_item); 
     std::swap(temp, *this); 
    } 
    return *this; 
} 

這可以簡化爲:

GroceryItem& GroceryItem::operator=(GroceryItem copy_item) 
{ 
    std::swap(copy_item, *this); 
    return *this; 
} 

當然,如果你還沒有實施過一個,那麼別忘了一個破壞者:

GroceryItem::~GroceryItem() 
{ 
    delete[] item_name; 
} 

operator>(),當然,因爲sort_items()期待一個。

現在,所有的說,如果你改變了item_name成員是一個std::string代替char*,你將不再需要手動執行析構函數,拷貝構造函數或複製賦值運算符在所有(只是默認構造函數來初始化數字成員)。編譯器的默認生成的析構函數的實現,拷貝構造函數和拷貝賦值運算符將足以用於管理所有的數據成員的你:

class GroceryItem 
{ 
public: 
    std::string item_name; 
    float item_price; 
    int qty_on_hand; 
    int qty_purchased; 

    GroceryItem(); 

    bool operator > (const GroceryItem& item) const; 
}; 

GroceryItem::GroceryItem() 
{ 
    item_price = 0.0f; 
    qty_on_hand = 0; 
    qty_purchased = 0; 
}; 

bool GroceryItem::operator > (const GroceryItem& item) const 
{ 
    return ...; 
} 

void sort_items(GroceryItem ini_customer_GroceryItem[], int number) 
{ 
    int j = 0, k = 0; 
    //GroceryItem temp2; 

    for (j = 0; j < number - 1; j++) // n-1 passes 
    { 
     for (k = number - 1; j < k; k--) // each pass runs one fewer than the preceding one 
     { 
      if (ini_customer_GroceryItem[k - 1] > ini_customer_GroceryItem[k]) 
      { 
       /* 
       temp2 = ini_customer_GroceryItem[k - 1]; 
       ini_customer_GroceryItem[k - 1] = ini_customer_GroceryItem[k]; 
       ini_customer_GroceryItem[k] = temp2; 
       */ 
       std::swap(ini_customer_GroceryItem[k - 1], ini_customer_GroceryItem[k]); 
      } 
     } 
    } 
} 
+0

比你所有的評論,我會改變我的代碼,看看它的工作。 – kolotei

+0

發現了一些問題與字符代碼, 'GroceryItem&GroceryItem ::運算符=(const的GroceryItem&copy_item) { 如果(這種= copy_item!) { GroceryItem溫度(copy_item); std :: swap(temp,* this); } return * this; }' – kolotei

+0

你看到什麼問題? –

相關問題