2017-10-21 47 views
-1

當我使用包含成員函數的普通函數時,我的程序不斷收到錯誤的alloc錯誤。拋出std :: bad_alloc的實例後調用Terminate。使用兩個類,其中一個指向另一個指針

該程序是關於從命令行獲取一些特定的輸入並打印指針數組的元素。這必須通過指針數組完成。

首先,我創建了一個需要2個字符串的類。一個用於名稱,另一個用於房間。然後,我創建了另一個具有大小和指向我的第一個類的指針,以創建一個數組。

我的主要是在最後,以上主要是2正常功能。這段代碼有什麼問題?當我第一次輸入命令時,它會運行,直到我輸入一個連接到普通函數的命令。那裏可能有些問題,但我似乎無法找到它。

#include <iostream> 
#include <string> 
using namespace std; 

class Address 
{ 
    private: 
     string name; 
     string room; 
    public: 
     Address(){}; 
     Address(string, string); 
     string get_name(); 
     string get_room(); 
     void change_room(string); 
}; 

Address::Address (string n, string r) 
{ 
    name = n; 
    room = r; 
} 


string Address::get_name() 
{ 
    return name; 
} 

string Address::get_room() 
{ 
    return room; 
} 

void Address::change_room(string change) 
{ 
    room = change; 
} 


//end of Address class 


class Address_Book 
{ 
    private: 
     int size; 
     Address* addresses; 
    public: 
     Address_Book(); 
     ~Address_Book(){ delete[] addresses;} 
     void add(Address); 
     void move(string, string); 
     int get_size(); 
     Address location(int); 
     int find(string); 
     void clear(); 
     void remove_address(string); 
     int exists(string); 
     void sort(); 
}; 


Address_Book::Address_Book() 
{ 
    int s = 0; 
    size = s; 
    addresses = new Address[s]; 
} 

void Address_Book::add(Address add) 
{ 
    Address* temp = new Address [size + 1]; 
    for (int i = 0; i < size; i++) 
    { 
     temp[i] = addresses[i]; 
    } 
    temp[size] = add; 
    delete[] addresses; 
    addresses = temp; 
    size ++; 

} 

void Address_Book::move(string name, string newroom) 
{ 
    for (int i = 0; i < size ; i++) 
    { 
     if (addresses[i].get_name() == name) 
     { 
      addresses[i].change_room(newroom); 
     } 
    } 
} 

void Address_Book::remove_address(string name) 
{ 
    Address* temp = new Address [size - 1]; 

    for (int i = 0; i < size; i++) 
    { 
     if (addresses[i].get_name() != name) 
     { 
      temp[i] = addresses[i]; 
     } 
     else if (addresses[i].get_name() == name) 
     { 
      for (int j = i + 1; j < size; j++) 
      { 
       temp[i] = addresses[j]; 
       i++; 
      } 
      break; 
     } 
    } 
    delete[] addresses; 
    addresses = temp; 
    size--; 
} 


int Address_Book::get_size() 
{ 
    return size; 
} 


Address Address_Book::location(int index) 
{ 
    return addresses[index]; 
} 


void Address_Book::sort() 
{ 
    Address temp; 
    for (int i = 0; i < size; i++) 
    { 
     for(int j = 0; j < size - 1; j++) 
     { 
      if (addresses[j].get_room() > addresses[j + 1].get_room()) 
      { 
       temp = addresses[j]; 
       addresses[j] = addresses[j + 1]; 
       addresses[j + 1] = temp; 
      } 
     } 
    } 
    for (int i = 0; i < size; i++) 
    { 
     if (addresses[i].get_room() == addresses[i + 1].get_room()) 
     { 
      if (addresses[i].get_name() > addresses[i + 1].get_name()) 
      { 
       temp = addresses[i]; 
       addresses[i] = addresses[i + 1]; 
       addresses[i + 1] = temp; 
      } 
     } 
    } 
} 

void Address_Book::clear() 
{ 
    Address * temp = new Address[0]; 
    delete[] addresses; 
    addresses = temp; 
    size = 0; 
} 


int Address_Book::find(string name) 
{ 
    for (int i = 0; i < size; i++) 
    { 
     if (addresses[i].get_name() == name) 
     { 
      return i; 
     } 
    } 
    return -1; 
} 



//end of Address_Book class 



void find(string name, Address_Book addbook) 
{ 
    int index = addbook.find(name); 
    cout << index << endl; 

    if (index > -1) 
    { 
     cout << addbook.location(index).get_name() << " is in room " << 
     addbook.location(index).get_room() << endl; 
    } 
    else 
    { 
     throw runtime_error("entry does not exist."); 
    } 
} 


void remove_add(string name, Address_Book book) 
{ 
    int exist = book.find(name); 

    if (exist > -1) 
    { 
     book.remove_address(name); 
    } 
    else 
    { 
     throw runtime_error("entry does not existt."); 
    } 

} 






int main() 
{ 
    Address_Book addbook; 
    string action, in_name, in_room; 
    do 
    { 
     try 
     { 
      cout << "> "; 
      cin >> action; 

      if (action == "add") 
      { 
       cin >> in_name >> in_room; 
       Address newadd(in_name, in_room); 
       addbook.add(newadd); 
      } 
      else if (action == "move") 
      { 
       cin >> in_name >> in_room; 
       addbook.move(in_name, in_room); 
      } 
      else if (action == "remove") 
      { 
       cin >> in_name; 
       remove_add(in_name, addbook); 
      } 
      else if (action == "find") 
      { 
       cin >> in_name; 
       find(in_name, addbook); 
      } 
      else if (action == "list") 
      { 
       addbook.sort(); 

       for (int i = 0; i < addbook.get_size(); i++) 
       { 
       cout << addbook.location(i).get_name() << " is in room 
       " << addbook.location(i).get_room() << endl; 
       } 
      } 
      else if (action == "clear") 
      { 
       addbook.clear(); 
      } 
      else 
      { 
       throw runtime_error("input mismatch."); 
      } 
     } 
     catch (runtime_error& e) 
     { 
      cerr << "error: " << e.what() << endl; 
     } 
    }while (action != "exit"); 

    return 0; 
} 
+0

將有助於瞭解你使用的是什麼操作系統,編譯器等。 導致問題的輸入示例會很有幫助。 此外,請讓我們知道什麼是在屏幕上打印。 – CplusPuzzle

+0

我必須重新創建這個向量。它是必須使用的。我需要了解記憶是如何工作的。 –

+0

操作系統是mac。當我輸入'add'後跟着名字和一個房間時,他們直接進入我創建的地址數組。當我輸入'list'時,它會打印整個數組。但是,當我想刪除或找到它只有第一次。那麼它會給我一個糟糕的分配()。這是怎麼回事? –

回答

0

函數remove_add需要通過引用或指針獲取地址簿對象。 它現在的樣子,從地址簿的副本中刪除。

它應該是這樣的:

void remove_add(string name, Address_Book& book) 
{ 
    int exist = book.find(name); 

    if (exist > -1) 
    { 
     book.remove_address(name); 
    } 
    else 
    { 
     throw runtime_error("entry does not existt."); 
    } 

} 

此外,你應該做的事情,在下面的功能的情況下size == 1不同。例如如果您的編譯器支持它,請將addresses設置爲NULL,0或nullptr

void Address_Book::remove_address(string name) 
{ 
    Address* temp = new Address[size - 1]; 

    for (int i = 0; i < size; i++) 
    { 
     if (addresses[i].get_name() != name) 
     { 
      temp[i] = addresses[i]; 
     } 
     else if (addresses[i].get_name() == name) 
     { 
      for (int j = i + 1; j < size; j++) 
      { 
       temp[i] = addresses[j]; 
       i++; 
      } 
      break; 
     } 
    } 
    delete[] addresses; 
    addresses = temp; 
    size--; 
} 

玩得開心學習語言和好運:)

+0

哦。現在我明白了......我非常感謝你,你能否請進一步解釋爲什麼會發生這種情況? –

+0

嘗試閱讀關於按值傳遞與傳遞參考的比較,將會有大量的解釋比我提供的要好。 – CplusPuzzle

0

導致你的問題的確切命令你的問題沒有規定,所以我周圍戳一點點,直到代碼段故障而墜毀。

ValgrindDr. Memory是找到這些問題的根本原因很棒的工具。你的情況:

$ g++ -g 46865300.cpp 
$ valgrind ./a.out 
> add foo bar 
> list 
==102== Invalid read of size 8 
==102== at 0x4EF4EF8: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) (in /usr/lib64/libstdc++.so.6.0.19) 
==102== by 0x401354: Address::get_room() (46865300.cpp:33) 
==102== by 0x401C05: Address_Book::sort() (46865300.cpp:152) 
==102== by 0x4026A3: main (46865300.cpp:262) 
==102== Address 0x5a17410 is 8 bytes after a block of size 24 alloc'd 
==102== at 0x4C2A8A8: operator new[](unsigned long) (vg_replace_malloc.c:423) 
==102== by 0x4014BF: Address_Book::add(Address) (46865300.cpp:74) 
==102== by 0x40245C: main (46865300.cpp:243) 

它說,下面的代碼進行了界外接入:

150  for (int i = 0; i < size; i++) 
151  { 
152   if (addresses[i].get_room() == addresses[i + 1].get_room()) 
153   { 
154    if (addresses[i].get_name() > addresses[i + 1].get_name()) 

我猜循環條件應該用「大小 - 1」而不是「大小」。

+0

這不是問題。嘗試輸入:'添加'John F450,然後添加其他人,例如'添加'弗蘭克T430。然後輸入'list'。然後輸入'find'John(它應該打印正確的輸出),但是當我再次輸入'list'或嘗試再次'查找'時,它會給出錯誤的分配或雙倍空閒或損壞 –

相關問題