gpt4 book ai didi

C++ 错误 C2228

转载 作者:行者123 更新时间:2023-11-28 07:27:13 24 4
gpt4 key购买 nike

我承认我不确定我在这里做什么,所以我从我的教科书上复制了很多示例代码并用我自己的程序的信息替换......但可以告诉我什么是导致此错误?

汽车.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

主要.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' 的左边必须有类/结构/union

错误 C2228:'.setMake' 的左边必须有类/结构/union

最佳答案

当您说 Car honda(int yearModel, string make); 时,您是在声明一个名为 honda 的函数,它接受一个 int 和一个字符串并返回一个 Car。要创建名为 honda 的 Car 变量,您需要使用实际值调用构造函数:

Car honda(2005, "Accord");

关于C++ 错误 C2228,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18555191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com