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 

主文件

#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);,你声明了一个名为 honda 的函数,它接受一个 int 和一个字符串并返回一个 Car。要创建名为 honda 的 Car 变量,您需要使用实际值调用构造函数:

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