我必须设计并实施一个程序来完成鼓棒匹配(而不是吃饭)。我必须检查两个参数,即两个不同的鼓槌的重量和音高(声学特性),并找到一对匹配的鼓槌。我创建了三个类,即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
我想将我的所有逻辑转移到主类,因为我的教授告诉我这个Robot类是假类(stubs),所以我创建了一些方法int load_sticks(int number)但是我有错误宣布
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;
}
答案 0 :(得分: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,这样你就不会忘记自己的位置,如果你发现自己走向死胡同,就可以撤退。