2010-09-19 40 views
0

我必須設計和實現一個程序來完成鼓棒匹配(而不是吃東西)。我必須檢查兩個參數,即兩個不同鼓槌的重量和音高(聲學屬性),並找到一對匹配的鼓槌。我創建了三個類,即Bin,Sorter和Stick,並在項目描述中給出了機器人類,這是存根類。存根和主程序

Robot 
+WEIGHT_TOLERANCE: int 
+PITCH_TOLERANCE: int 
+get_new_bin(void): bool 
+is_bin_empty(void):bool 
+load_sticks(number:int):int 
+test_stick(location: int, pitch: int, weight: int): bool 
+pair_sticks(first_location: int, second_location: int): bool 
+return_sticks(void): bool 

我想我所有的邏輯轉移到主類,因爲我的教授告訴我,這個機器人類是假的類(存根),所以我創造了一些方法INT load_sticks(INT數),但我的在聲明中發生錯誤

my_sorter = new Sorter(); 
my_bin = new Bin(); 

在主類中。我不知道如何在主類中實例化這些對象。 任何人都可以告訴我該怎麼辦?

這是我的Robot類源文件的代碼。我也可以發佈我的其他課程,但我只想解決這個問題。我希望只有這個階級能理解。

#include "Robot.h" 

#include<iostream> 
#include <string> 
#include "Sorter.h" 
#include "Bin.h" 
#include "Stick.h" 

using namespace std; 

// Default constructor 
Robot::Robot(void) 
{ 
    WEIGHT_TOLERANCE = 3; 
    PITCH_TOLERANCE = 20; 
    my_sorter = new Sorter(); 
    my_bin = new Bin(); 
} 

我刪除了所有其他的方法,所以,這將是可讀的所有

// Load sticks into the sorter from the bin 

int Robot::load_sticks(int number){ 
    // Declare and initialize the int sticks_loaded to be returned 
    int sticks_loaded = 0; 
    // Verify the number of sticks requested to be loaded doesn't exceed the number in the bin 
    //if(number < my_bin->get_size()){ 
    /* If the number of sticks requested plus the current size of the sorter is less than the sorter's maximum 
    * capacity, add the number of sticks requested 
    */ 
    if((my_sorter->get_size() + number) <= 200) 
    { 
     sticks_loaded = number; 

     // Load the number of sticks requested 
     for(int i = 0; i < number; i++) 
     { 
      // Call the load() method in the sorter class to add each stick to the sorter 
      my_sorter->load(my_bin->get(i)); 
     } 

     // Remove the sticks that have been added to the sorter from the bin 
     my_bin->remove(0, number); 

     // After the sticks have been loaded, sort the sticks in the sorter 
     my_sorter->sort_sticks(); 

     // Print out the contents of the sorter after loading 
     cout << *my_sorter << endl; 
    } 

    /* If the number requested plus the current size of the sorter exceeds the sorter's capacity, 
    * add sticks to the sorter until it has reached it's capacity 
    */ 
    if((my_sorter->get_size() + number) > 200) 
    { 
     cout << "Requested too many!" << endl; 
     // sticks_loaded = the maximum capacity of the sorter minus it's current size 
     sticks_loaded = 200-(my_sorter->get_size()); 

     cout << "Sticks can load: " << sticks_loaded << endl; 
     for(int i = 0; i < sticks_loaded; i++) 
     { 
      // Load the sticks from the bin into the sorter 
      my_sorter->load(my_bin->get(i)); 
     } 

     // Remove the sticks which were added to the sorter from the bin 
     my_bin->remove(0, sticks_loaded); 

     // Sort the sticks after loading 
     my_sorter->sort_sticks(); 

     // Output the contents of the sorter after loading 
     cout << *my_sorter << endl; 

    } 

    return sticks_loaded; 
} 
+1

-1 - 每發佈一個問題都不會「酷」生成新的SO ID – 2010-09-19 14:22:19

+0

我沒有故意這樣做,無法弄清楚如何打開我的帳戶。 – cool 2010-09-19 14:25:40

+0

所以這次我註冊了。對造成的不便表示歉意。 – cool 2010-09-19 14:26:59

回答

0

我建議你自己創建一個最小的測試用例,而不大部分基礎設施,只是集中在爲什麼my_sorter = new Sorter();語句失敗。

#include <Sorter.h> 
void dummy(void) 
{ 
    Sorter *my_sorter = new Sorter(); 
    delete my_sorter; 
} 

這是編譯嗎?如果沒有,請修復它。如果是這樣,它複雜化:

#include <Sorter.h> 
struct x { Sorter *my_sorter; }; 
void dummy(void) 
{ 
    x dummy; 
    x.my_sorter = new Sorter(); 
    delete x.my_sorter; 
} 

等等

我注意到,Sorter.h應該是獨立的 - 有人用頭中定義的類應該不需要任何其他頭預先包括在內。

如果問題還不是不言而喻的,則重複Bin類。並從那裏發展。確保你使用的是VCS,這樣你就不會失去自己的位置,並且如果你發現自己走到了一條死巷,就會退縮。

+0

我現在就要這樣做。 – cool 2010-09-19 16:03:03