0

ここで何をしているのかわからないので、教科書からサンプルコードをコピーして自分のプログラムの情報に置き換えることをたくさん行っています...しかし、このエラーの原因を教えてください?

車.cpp

// Implementation file for the Car class
#include "Car.h"

// This constructor accepts arguments for the car's year 
// and make. The speed member variable is assigned 0.
Car::Car(int carYearModel, string carMake)
{
    yearModel = carYearModel;
    make = carMake;
    speed = 0;
}

// Mutator function for the car year
void Car::setYearModel(int carYearModel)
{
        carYearModel = yearModel;
}

// Mutator function for the car make
void Car::setMake(string carMake)
{
    carMake = make;
}

車.h

// Specification file for the Car class
#ifndef CAR_H
#define CAR_H
#include <string>
using namespace std;

class Car
{
private:
    int yearModel; // Car year model
    string make;   // Car make
    int speed;     // Car speed

public:
    Car(int, string); // Constructor

    // Mutators
    void setYearModel(int);
    void setMake(string);

};

#endif 

main.cpp

#include <iostream>
#include <iomanip>
#include <string>
#include "Car.h"
using namespace std;

int main()
{
    // Create car object
    Car honda(int yearModel, string make);

    // Use mutator functions to update honda object

    honda.setYearModel(2005);
    honda.setMake("Accord");


    return 0;
}

これらは私が得ているエラーです:

エラー C2228: '.setYearModel' の左側にはクラス/構造体/共用体が必要です

エラー C2228: '.setMake' の左側にはクラス/構造体/共用体が必要です

4

1 に答える 1

1

と言うときCar honda(int yearModel, string make);は、int と文字列を取り、Car を返す honda という名前の関数を宣言しています。honda という名前の Car 変数を作成するには、コンストラクターを実際の値で呼び出す必要があります。

Car honda(2005, "Accord");
于 2013-09-01T04:30:09.847 に答える