Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.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++ I';我正在设计一个汽车设计程序,并试图遵循一系列限制_C++ - Fatal编程技术网

C++ I';我正在设计一个汽车设计程序,并试图遵循一系列限制

C++ I';我正在设计一个汽车设计程序,并试图遵循一系列限制,c++,C++,我试图遵循这些限制:1)只能打印从car对象获得的字符串文本和值,2)只能有一个car&参数,3)不能调用setSpeed()成员 到目前为止,没有成功。我希望你们能带我走过去 我目前只有两个错误,但当错误出现时,我看到了太多的错误,我相信我可能只能通过显示屏幕截图给你一个想法 这是我的主要节目 //Car design #include <iostream> #include "Car header.h" #include <string&g

我试图遵循这些限制:1)只能打印从car对象获得的字符串文本和值,2)只能有一个car&参数,3)不能调用setSpeed()成员

到目前为止,没有成功。我希望你们能带我走过去

我目前只有两个错误,但当错误出现时,我看到了太多的错误,我相信我可能只能通过显示屏幕截图给你一个想法

这是我的主要节目

    //Car design
    #include <iostream>
    #include "Car header.h"
    #include <string>
    #include <stdlib.h>

    //function prototypes
    void findSpeedGoingUp (int &speed, int yearModel, string make);
    void findSpeedGoingDown(int &speed, int yearModel, string make);


    using namespace std;

    //global constants
    const int NUM_FOR_INCREASE=80;
    const int NUM_FOR_CAL=5;
    const int ONE_FOR_CAL=1;
    int main()
    {
        //some variables
        int yearModel;
        string make;
        int speed=0;


        //calling the functions
        findSpeedGoingUp(speed, yearModel, make);
        findSpeedGoingDown(speed, yearModel, make);


        system("Pause");
        return 0;
    }//end main

    //function for increasing speed
    void findSpeedGoingUp(int &speed, int yearModel, string make)
    {
        Car carObject(yearModel, make);



            //user input
        cout << "Enter the car model year" << endl;
        cin >> carObject.setYearModel(yearModel);
        cout << "Enter the cars make" << endl;
        cin >> carObject.setMake(make);
        cout << "The model year is " << carObject.getYearModel()<< endl;
        cout << "The make is " << carObject.getMake() << endl;
        cout << "The speed is " << carObject.getSpeed()<< endl;
        cout << "Let's see what it can do." << endl;
        cout << "The speed is,..." << endl;

        for(int n=50; n<NUM_FOR_INCREASE; ++n)
        {
            carObject.accelerate();

            speed=speed+NUM_FOR_CAL;
            cout << carObject.getSpeed() << " ";
        }//end for loop
        cout << "\nStop! Stop! Let me out." << endl;
    }//end findSpeedGoingUp

    //function for decreasing speed
    void findSpeedGoingDown(int &speed, int yearModel, string make)
    {
        Car carObject(yearModel, make);

        for(int n = 50; n < NUM_FOR_INCREASE + ONE_FOR_CAL; ++n)
        {
            carObject.brake();

            cout << carObject.getSpeed() << " ";

            speed = speed - NUM_FOR_CAL;
        }//end for loop
        cout << "\nI'll walk from here." << endl;
    }//end findSpeedGoingDown
//汽车设计
#包括
#包括“轿厢头部.h”
#包括
#包括
//功能原型
void findSpeedGoingUp(int&speed,int yearModel,string make);
void findSpeedGoingDown(int&speed、int-yearModel、字符串生成);
使用名称空间std;
//全局常数
const int NUM_,用于增加=80;
const int NUM_表示_CAL=5;
const int ONE_表示_CAL=1;
int main()
{
//一些变量
国际模型;
串制;
int速度=0;
//调用函数
findSpeedGoingUp(速度、年份型号、品牌);
findSpeedGoingDown(速度、年份型号、品牌);
系统(“暂停”);
返回0;
}//端干管
//提速功能
void findSpeedGoingUp(整数和速度、整数年模型、字符串生成)
{
carObject(车型、品牌);
//用户输入
cout carObject.setYearModel(yearModel);
cout carObject.setMake(make);

不能您不能
cin>>affunction()

您必须定义一个临时变量

int ym;
cin >> ym;
cin >> carObject.setYearModel(ym);
您的代码中有太多错误,我不得不更改很多内容以获得预期的输出:

Enter the car model year
1970
Enter the cars make
renault
The model year is 1970
The make is renault
The speed is 0
Let's see what it can do.

The speed is,...
10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250 260 270 2
80 290 300
Stop! Stop! Let me out.
295 285 275 265 255 245 235 225 215 205 195 185 175 165 155 145 135 125 115 105 95 85 75 65 55 45 35
 25 15 5 -5
I'll walk from here.
Car.cpp
现在包含以下内容(糟糕的设计,主管道应该在其他地方,好吧!):

简而言之,我在你的代码中修正了什么

  • 在提示用户输入品牌和年份后,在
    中创建了
    汽车
    的唯一实例
  • 将速度更改方法移动到对象本身,更有意义
  • 这允许我在对象本身上直接调用
    getSpeed()
    等方法,而无需实例名
  • 这使我能够在连贯的结果中提高速度,然后降低速度(您实例化了两个不同的对象,因此得到了负值)
我没有在你的代码中修复什么(最适合代码审查)

  • 在参数中传递
    string
    会产生一个副本,这对性能有害,更喜欢通过常量引用传递
    const string&

    void setMake(常量字符串和aMake)
    {
    制造=制造;
    }

  • getter方法应该具有
    const
    属性,以便能够对常量引用进行调用

    const字符串&getMake()const{return make;}
    int getYearModel()常量
    {
    回归模型;
    }


您遇到的具体错误是什么?我只给您显示了一个链接。哈哈。这是两个错误。@sebastianlenartowicz您可以(也应该)做到这一点将错误文本复制/粘贴到您的问题中。它们比图片可读性强得多,并且在链接过期时不会消失。Fabre为什么我不能主要使用yearModel和其他变量来完成此操作?TW您正在构建对象并稍后以交互方式设置属性。有什么用?我不明白您在说什么。我的课教得很差,对我来说太无聊了。@Jean-Francois FabreI收到了一个奇怪的输出@Jean-Francois Fabrey你似乎不懂对象的概念。让我再次编辑我的帖子。
int ym;
cin >> ym;
cin >> carObject.setYearModel(ym);
Enter the car model year
1970
Enter the cars make
renault
The model year is 1970
The make is renault
The speed is 0
Let's see what it can do.

The speed is,...
10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250 260 270 2
80 290 300
Stop! Stop! Let me out.
295 285 275 265 255 245 235 225 215 205 195 185 175 165 155 145 135 125 115 105 95 85 75 65 55 45 35
 25 15 5 -5
I'll walk from here.
//Car design
#include <iostream>
#include "Car.h"
#include <string>
#include <stdlib.h>
using namespace std;

const int NUM_FOR_INCREASE=80;
const int NUM_FOR_CAL=5;
const int ONE_FOR_CAL=1;


//function for increasing speed
void Car::findSpeedGoingUp()
{  

   cout << "The model year is " << getYearModel()<< endl;
    cout << "The make is " << getMake() << endl;
    cout << "The speed is " << getSpeed()<< endl;
    cout << "Let's see what it can do." << endl;
    cout << "The speed is,..." << endl;

    for(int n=50; n<NUM_FOR_INCREASE; ++n)
    {
        accelerate();

        speed+=NUM_FOR_CAL;
        cout << getSpeed() << " ";
    }//end for loop
    cout << "\nStop! Stop! Let me out." << endl;
}//end findSpeedGoingUp

//function for decreasing speed
void Car::findSpeedGoingDown()
{


    for(int n = 50; n < NUM_FOR_INCREASE + ONE_FOR_CAL; ++n)
    {
        brake();

        cout << getSpeed() << " ";

        speed = speed - NUM_FOR_CAL;
    }//end for loop
    cout << "\nI'll walk from here." << endl;
}//end findSpeedGoingDown

//global constants
int main()
{
    int yearModel;
    string make;

    //some variables
      //user input
    cout << "Enter the car model year" << endl;
    cin >> yearModel;
    cout << "Enter the cars make" << endl;
    cin >> make;
    Car carObject(yearModel, make);        


    //calling the functions
    carObject.findSpeedGoingUp();
    carObject.findSpeedGoingDown();


    system("Pause");
    return 0;
}//end main
//Car design header
#ifndef CAR_HEADER_H_INCLUDED
#define CAR_HEADER_H_INCLUDED
#include <string>

using namespace std;

class Car
{
private:
    //private fields to hold data
    int yearModel;
    string make;
    int speed;

    //constructor
public:

    //constructor
    Car(int ym, string mk)
    {
        yearModel=ym;
        make=mk;
        speed=0;
    }//end Car

    void setYearModel(int aYearModel)
    {
        yearModel=aYearModel;

    }//end setYearModel


    int getYearModel()
    {
        return yearModel;
    }//end returnYearModel

    void setMake(string aMake)
    {
        make=aMake;
    }//end setMake

    string getMake()
    {
        return make;
    }//end returnMake

    void setSpeed(int aSpeed)
    {
        speed=aSpeed;
    }//end setSpeed

    int getSpeed()
    {
        return speed;
    }//end returnSpeed

    //methods to use in main program

    void accelerate()
    {
        speed += 5;
    }// accelerate

    void brake()
    {
        speed -= 5;
    }//end brake
    void findSpeedGoingUp();
    void findSpeedGoingDown();

};//end Car
#endif // CAR_HEADER_H_INCLUDED