Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 存根和主程序_C++ - Fatal编程技术网

C++ 存根和主程序

C++ 存根和主程序,c++,C++,我必须设计和实现一个程序来完成鼓棒匹配(而不是吃的那个)。我必须检查两个参数,即两个不同鼓槌的重量和音高(声学特性),并找到一对匹配的鼓槌。我创建了三个类,即Bin、Sorter和Stick,在项目描述中给出了Robot类,这是stub类 Robot +WEIGHT_TOLERANCE: int +PITCH_TOLERANCE: int +get_new_bin(void): bool +is_bin_empty(void):bool +load_sticks(number:int):int

我必须设计和实现一个程序来完成鼓棒匹配(而不是吃的那个)。我必须检查两个参数,即两个不同鼓槌的重量和音高(声学特性),并找到一对匹配的鼓槌。我创建了三个类,即Bin、Sorter和Stick,在项目描述中给出了Robot类,这是stub类

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
我想把我所有的逻辑转移到主类,因为我的教授告诉我这个机器人类是假的类(stubs),所以我创建了一些方法int load_sticks(int number),但是我在声明时出错了

my_sorter = new Sorter();
my_bin = new Bin();
在主课上。我不知道如何在主类中实例化这些对象。 谁能告诉我该怎么处理这个

这是我的机器人类源文件代码。我也可以发布我的其他课程,但我只想解决这个问题。我希望只有通过这门课才能理解

#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();
}
#包括“Robot.h”
#包括
#包括
#包括“Sorter.h”
#包括“Bin.h”
#包括“Stick.h”
使用名称空间std;
//默认构造函数
Robot::Robot(void)
{
重量公差=3;
螺距公差=20;
my_分拣机=新分拣机();
my_bin=新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;
}
//将木棍从箱子装入分拣机
int Robot::加载_杆(int编号){
//声明并初始化要返回的加载的int\u
加载的int_=0;
//验证请求装载的棍棒数量没有超过箱子中的数量
//如果(数字get\u size()){
/*如果请求的棒数加上分拣机的当前尺寸小于分拣机的最大值
*容量,添加请求的棒数
*/
如果((我的分拣机->获取大小()+编号)装载(我的垃圾箱->获取(i));
}
//从料仓中取出添加到分拣机的棍棒
my_bin->删除(0,编号);
//装载棍棒后,在分拣机中分拣棍棒
my_sorter->sort_sticks();
//装载后打印出分拣机的内容
cout(200)
{ 

cout我建议您自己创建一个没有大部分基础设施的最小测试用例,只关注
my_sorter=new sorter();
语句失败的原因

#include <Sorter.h>
void dummy(void)
{
    Sorter *my_sorter = new Sorter();
    delete my_sorter;
}
#包括
虚空假人(虚空)
{
分拣机*my_分拣机=新分拣机();
删除我的分拣机;
}
是否编译?如果不编译,请修复。如果编译,请使其复杂化:

#include <Sorter.h>
struct x { Sorter *my_sorter; };
void dummy(void)
{
    x dummy;
    x.my_sorter = new Sorter();
    delete x.my_sorter;
}
#包括
结构x{Sorter*my_Sorter;};
虚空假人(虚空)
{
x假人;
x、 my_分拣机=新分拣机();
删除x.my_分拣机;
}
等等

我注意到Sorter.h应该是自包含的——使用标头中定义的类的人不需要预先包含任何其他标头


如果问题还不是不言而喻的,那么对Bin类重复上述步骤。然后继续进行。确保你使用的是VCS,这样你就不会迷失方向,如果你发现自己走上了死胡同,就可以撤退。

-1-这不是“酷”若要在每次发布问题时生成一个新的SO ID,我不是故意这样做的,我不知道如何打开我的帐户。我格式化了您的代码以使其可读-请下次使用编辑器中的
101010
按钮缩进您的代码,或者使用
101010
按钮。此外,为了您自己的最大利益,请尝试减少已发布代码的数量至少足以说明这个问题——你上面的代码太多了,可能会阻止大多数读者尝试帮助你……另一件事:你确定你的教授提到的是“螺柱”而不是“存根”吗?我从未在IT上下文中听到过前者;-)问题到底是什么?为什么不能从“主类”实例化该类你是否会得到一个编译器错误?你应该能够将代码减少到比这更小的东西。我马上注意到:这是C++,你不需要用新的方法来创建堆上的所有东西,你也不应该这样做。这行:<代码> MyIOSLeTe=源代码。MyoSalter;< /Cord>不按你的想法去做。oes;它只复制指针;它不调用
分类器
复制构造函数。