首页 文章

第一次使用C语言中的类:请求非类型成员

提问于
浏览
-2

我按照你们告诉我的内容编辑了这个 . 它一直到这一行和错误

cout <<“起始速度是”<< honda.getSpeed()<< endl << endl

[错误]请求'honda'中的成员'getSpeed',这是非类型'Car(int,std :: string,int){aka Car(int,std :: basic_string,int)}'

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Car  {
private:
    int yearModel;
    string make;
    int speed;

public:
    Car(int carYearModel, string carMake, int carSpeed = 0) {
        yearModel = carYearModel;
        make = carMake;
        speed = carSpeed; 
    }

    void accelerate() {
        speed += 5;
    }

    void brake() {
        speed -= 5;
    }

    int getSpeed() {
        return speed; 
    }

    int getyearModel() {
        return yearModel;
    }

    string getMake() {
        return make; 
    }
 };

int main() {

int count, YearModel, speed, make;
string carMake;

cout <<  "What is the speed?";
cin  >>  speed;

cout << "What is the year of the model?";
cin  >> YearModel;

cout << "What is the make?";
cin  >> make;

Car honda(int carYearModel, string carMake, int carSpeed);

cout << "The starting speed is " << honda.getSpeed() << endl << endl;
cout << "The year and model is  " << honda.getyearModel() << endl << endl;
cout << "The make of the car is "  << honda.getMake() << endl << endl;

return 0;
}

2 回答

  • 2

    您的getter方法不应该要求用户输入 .

    此外,它应该是汽车本田(carYearModel,carMake,carSpeed);并且您需要为变量carYearModel,carMake`和carSpeed赋值,然后才能将它们用作方法参数 . 您可以从用户那里获取这些值 . 调用方法时不包括参数数据类型 . 您只在定义方法时才这样做 .

    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    class Car  {
        private:
            int yearModel;
            string make;
            int speed;
    
        public:
            Car(int carYearModel, string carMake, int carSpeed = 0) {
                yearModel = carYearModel;
                make = carMake;
                speed = carSpeed; 
            }
    
            void accelerate() {
                speed += 5;
            }
    
            void brake() {
                speed -= 5;
            }
    
            int getSpeed() {
                return this.speed; 
            }
    
            int getyearModel() {
                return this.yearModel;
            }
    
            string getMake() {
                return this.make; 
            }
    };
    
    int main() {
    
        int count, carYearModel, carSpeed;
        string carMake;
    
        cout <<  "What is the speed?";
        cin  >> carSpeed;
    
        cout << "What is the year of the model?";
        cin  >> carYearModel;
    
        cout << "What is the make?";
        cin  >> carMake;
    
        Car honda(carYearModel, carMake, carSpeed);
    
        cout << "The starting speed is " << honda.getSpeed() << endl << endl;
        cout << "The year and model is  " << honda.getyearModel() << endl << endl;
        cout << "The make of the car is "  << honda.getMake() << endl << endl;
    
        return 0;
    }
    
  • 1
    Car honda(int carYearModel, string carMake, int carSpeed);
    

    声明一个名为 honda 的函数,它接受三个参数返回一个 Car . 你需要的是:

    int carYearModel = 2015;    // Some value
    string carMake =  "Honda";  // Makes sense to define honda
    int carSpeed = 155;         // Some value
    
    // Create an object of type Car
    Car honda(carYearModel, carMake, carSpeed);
    

相关问题