2015-10-07 53 views
-2

我在这里遇到了一些麻烦。我不知道我做错了什么。我的bike.cpp类很好。但我认为问题出在bike_shed类,我遇到了“park”和“checklegal”方法的问题。我们被要求做这样的事情:“有10个默认构造的自行车对象的私有数组BikeShed该类的类应该具有以下公共方法:由编译器提供Objects Array - C++

  • 一个默认的构造BikeShed() 。
  • 函数布尔公园(常量自行车&)增加了一个自行车可用 现场并返回true,如果BikeShed已满,则函数返回 假。

  • 函数自行车删除(常量字符串& int),重新移动并返回 与给定名称的所有者的第一辆自行车。如果找到这样的自行车不是 ,则该功能返回一辆自行车“无”。

  • 一个函数bool checkLegal(),如果所有非“None”的自行车 合法,它将返回true。如果发现一辆自行车违章 打印一条信息打印自行车。 。

  • 一个函数void print()函数,打印所有的自行车与业主 不是 “无” 等”

这里是我的代码:

这里是bike_shed.cpp文件

#include <iostream> 
#include "bike_shed.h" 
#include "Bike.h" 

using namespace std; 

void bike_shed::print(){ 

    cout<< "Bike: " << sizeof(Bike) <<endl; 

} 

bool bike_shed::checkLegal() { 

    Bike bike1; 

    if(bike1.getOwner() == "None"){ 
     return false; 
    } 
    else{ 
     return true; 
    } 

} 

//Bike bike_shed::remove(const string&, int) { 
// 
// 
//} 

bool bike_shed::park(const Bike&) { 

     if (sizeof(Bike) > 10) { 
      return false; 
     } 

} 

这里是Bike.cpp文件

#include "Bike.h" 
#include <iostream> 

using namespace std; 


void Bike::setNLight(int _light) { 
    d_nLight = _light; 
} 

void Bike::setBell(bool _bell) { 
d_bell = _bell; 
} 

void Bike::setOwner(string _owner) { 
d_owner = _owner; 
} 

void Bike::setReflector(bool _reflector) { 
    d_reflector = _reflector; 
} 

int Bike::getNLight() { 
    return d_nLight; 
} 

string Bike::getOwner() { 
    return d_owner; 
} 

bool Bike:: hasReflector() { 

    if (d_reflector == true) { 
     return true; 
    } 
    else { 

     return false; 
    } 
} 

    bool Bike:: hasBell(){ 

     if(d_bell == true) { 
      return true; 
     } 
     else{ 
      return false; 
     } 
    } 

bool Bike::isLegal() { 
    if (d_nLight >= 1 && d_reflector && d_bell) { 
     return true; 
    } 
    else { 
     return false; 
    } 
} 

void Bike::print() { 
    cout << "Owner: " << d_owner << " Color: " << d_color.Red << " " << d_color.Green << " " << d_color.Blue 
    << " " << " Lights: " << d_nLight << " Bell: " << d_bell << " Reflector: " <<d_reflector << endl; 
} 

Bike::Bike(string name, Color color){ 
    d_owner = name; 
    d_color = color; 
} 

这里是bike_shed.h文件

#include "Bike.h" 

class bike_shed { 

public: 

bike_shed(); 
bool park(const Bike&); 
Bike remove(const string&, int); 
bool checkLegal(); 
void print(); 


public: 
Bike bike[10];}; 

我会很感激,如果有人能帮助我。谢谢:)

+0

您可能想要澄清remove方法的签名。 int是什么?在顶部的描述中,你没有在'string'和'int'之间加逗号。 –

回答

1

在下面的代码块,

if (sizeof(Bike) > 10) { 
     return false; 
    } 

我猜测你想确保你不超过10个自行车停放在车棚。

为了做到这一点,您需要在park_shed有一个成员变量来指示停放在棚内的自行车的数量。然后,你可以使用:

bool bike_shed::park(const Bike& bike) 
{ 
    if (number_of_parked_bikes < 10) 
    { 
     bikes[number_of_parked_bikes] = bike; 
     ++number_of_parked_bikes; 
     return true; 
    } 
    else 
    { 
     retun false; 
    } 
} 

确保在构造函数初始化number_of_parked_bikes为零。

checkLegal功能会是这样的:

// Make it a `const` member function since it does not 
// change anything in bike_shed. 
bool bike_shed::checkLegal() const 
{ 
    bool isLegal = true; 
    for (int i = 0; i < number_of_parked_bikes; ++i) 
    { 
     if (bikes[i].getOwner() == "None") 
     { 
     // No need to check whether this bike is legar or not. 
     } 
     else if (!bikes[i].isLegal()) 
     { 
     isLegal = false; 
     cout << "Illegal bike found.\n"; 
     bikes[i].print(); 
     } 
    } 

    return isLegal; 
} 
+0

拆卸自行车时,您可能还需要压缩阵列。简单的做法是将最后一个元素交换到已删除元素的位置(或者,如果已删除元素是最后一个元素,则不执行任何操作)。这是因为在这个答案中隐含的假设是“自行车数量”是新车总是添加的地方,这并不是一个错误的假设。 –

+0

你的意思是在pard_shed中有一个成员变量。我只是做,自行车park_shed; ? – user3242013

+0

在'park_shed'类中,在'Bike bike [10];'之后添加一行'int number_of_parked_bikes;'。 –

0

在方法checkLegal(),你可能要检查一个给定的自行车是否有效不插入之前,所以你可以写这样的:

bool bike_shed::checkLegal(const Bike &bike1) { 

    if(bike1.getOwner() == "None"){ // or maybe bike1.isLegal() 
     return false; 
    } 
    else{ 
     return true; 
    } 
} 

此外,如果你想打印在bike_shed你可能想写点东西像所有的自行车:

void bike_shed::print(){ 

    for (int iBike = 0; iBike < nbBikes < iBike++) 
     bike[iBike].print(); 

} 
+0

为什么你将Const Bike&bike1添加到checkLegal方法?它不应该采取任何参数。 – user3242013

+0

以及变量nbBikes,那是什么?是自行车还是自行车类型?像,自行车nbBikes;或bike_shed nbBikes; ? – user3242013

+0

如果我为checkLegal做了这样的事情,我得到一个错误,说“在常量函数上调用非常量函数getOwner'。 – user3242013