开发者

Stubs and main program

I have to design and implement a program to accomplish drum stick matching (not the eating one). There are two parameters I have to check i.e Weight and Pitch (acoustical property) for the two different drumstick and find pair of matching drumstick. I created three classes i.e Bin, Sorter and Stick and in project description Robot class is given and this is stub class.

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

I want to shift all my logic to the main class, since my professor told me that this Robot class is fake class (stubs), so I created some of the method int load_sticks(int number) but I am having error in declaring

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

in the main class. I do not know how can I instantiate these object in main class. Could any one tell me what should I do with this?

This is my code for Robot class source file. I can post my other classes also but I just want to work on this problem. I hope one can understand with this class only.

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

I removed all other methods so that it will be readable to all

// 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
    开发者_JAVA百科    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;
}


I suggest you create yourself a minimal test case without most of the infrastructure, just concentrating on why the my_sorter = new Sorter(); statement fails.

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

Does this compile? If not, fix it. If so, complicate it:

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

Etc.

I note that Sorter.h should be self-contained - someone using the class defined in the header should not need any other headers pre-included.

Repeat for the Bin class if the problem isn't already self-evident. And progress from there. Make sure you're using a VCS so that you don't lose track of where you are and can retreat if you find yourself headed down a blind alley.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜