2016-08-03 23 views
1

嘿傢伙,所以我被分配到調試和修復給定的代碼。在我們修復它之後,這個任務應該是這樣工作的: 在創建Car對象的程序中演示該類,然後調用五次加速函數 。每次調用加速功能後,獲取當前的車速並顯示它。然後,調用制動功能六次。在每次撥打剎車功能後,獲取當前的車速並顯示它爲什麼不在制動範圍內宣佈制動和加速?

這就是我所擁有的 - 問題是一旦運行我得到一個錯誤,說「加速」和「剎車未聲明在範圍內這個範圍「這很奇怪,因爲它們的功能應該放在正確的位置。讓我知道如果我錯過了什麼謝謝!

#include <math.h> 

#include <iostream> 
#include <iomanip> 

#include <cstring> 
#include <cstdlib> 

using namespace std; 
class Car 
{ 
private: 
    int YearModel; 
    int Speed; 
    string Make; 
public: 
    Car(int, string, int); 
    string getMake(); 
    int getModel(); 
    int getSpeed(); 
    int Accelerate(int aSpd); 
    int Brake(int bSpd); 
    void displayMenu(); 
}; 
Car::Car(int YearofModel, string Makeby, int Spd) 
{ 
    YearModel = YearofModel; 
    Make = Makeby; 
    Speed = Spd; 
} 
string Car::getMake() 
{ 
    return Make; 
} 
//To get the year of the car. 
int Car::getModel() 
{ 
    return YearModel; 
} 
//To holds the car actual speed. 
int Car::getSpeed() 
{ 
    return Speed; 
} 
//To increase speed by 5. 
int Car::Accelerate(int aSpd) 
{ 
    aSpd = Speed; 
    Speed = Speed + 5; 
    return aSpd; 
} 
//To drop the speed of the car by 5. 
int Car::Brake(int bSpd) 
{ 
    bSpd = Speed; 
    Speed = Speed - 5; 
    return bSpd; 
} 
void displayMenu() 
{ 
    cout << "\n Menu\n"; 
    cout << "----------------------------\n"; 
    cout << "A)Accelerate the Car\n"; 
    cout << "B)Push the Brake on the Car\n"; 
    cout << "C)Exit the program\n\n"; 
    cout << "Enter your choice: "; 
} 
int main() 
{ 
    int Speed = 0; //Start Cars speed at zero. 
    char choice; //Menu selection 
    int year; 
    string carModel; 
    cout << "Enter car year: "; 
    cin >> year; 
    cout << "Enter the car model(without spaces): "; 
    cin >> carModel; 


    Car first(year, carModel, Speed); 

    //Display the menu and get a valid selection 
    do 
    { 
     displayMenu(); 
     cin >> choice; 
     while (toupper(choice) < 'A' || toupper(choice) > 'C') 
     { 
      cout << "Please make a choice of A or B or C:"; 
      cin >> choice; 
     } 
     //Process the user's menu selection 


     switch (choice) 
     { 
     case 'a': 
     case 'A': cout << "You are accelerating the car. "; 
     cout << Accelerate(first) << endl; 
     break; 
     case 'b': 
     case 'B': cout << "You have choosen to push the brake."; 
      cout << Brake(first) << endl; 
      break; 
     } 
    }while (toupper(choice) != 'C'); 


    return 0; 
    system("pause"); 

} 

回答

4

應該

first.Accelerate(speed) 

這是Accelerate聲明:

int Car::Accelerate(int aSpd) 

這是需要一個int作爲參數的Car的方法。

但你正在做的是:

Accelerate(first) 

這是一個函數調用(一個叫Accelerate是未申報的功能),以及你傳遞一個Car

0

作爲AccelerateBreakCar的功能,它們必須在Car對象上被調用:first.Accelerate(42)

當您在main函數中使用AccelerateBreak時,它們被稱爲自由函數,它不存在。


在你的情況並不重要,你傳遞給AcceleateBreak,作爲參數被覆蓋,從來沒有使用過什麼樣的價值觀:

int Car::Accelerate(int aSpd) 
{ 
    aSpd = Speed; 
    // ... 
} 
+0

那好吧甜,修復了。但是現在當我運行程序時,它只是讓我輸入模型並製作(這很奇怪),然後顯示3和0,然後按任意鍵然後完成...不應該顯示菜單和無論我想加速還是休息? –

+0

我說它很奇怪,因爲在int main中我有它,所以我把輸入車年然後模型。不是在同一時間... –

+0

所以應該這樣寫?汽車第一=新車(年份,車型,速度); 如果是這樣說它從車*轉換到非標量型車請求錯誤\ –