[英]C++ error C2228
I'll admit I'm not sure of what I'm doing here, so I am doing a lot of copying sample code from my textbook and replacing with info for my own program... but can tell me what is causing this error? 我承认我不确定自己在这里做什么,所以我做了很多工作,从教科书中复制示例代码,并用自己程序的信息代替...但是可以告诉我是什么导致了此错误?
Car.cpp Car.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;
}
Car.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 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;
}
These are the errors I'm getting: 这些是我得到的错误:
error C2228: left of '.setYearModel' must have class/struct/union 错误C2228:“。setYearModel”的左侧必须具有class / struct / union
error C2228: left of '.setMake' must have class/struct/union 错误C2228:“。setMake”的左侧必须具有class / struct / union
When you say Car honda(int yearModel, string make);
当你说
Car honda(int yearModel, string make);
, you're declaring a function named honda that takes an int and a string and returns a Car. ,您要声明一个名为honda的函数,该函数需要一个int和一个字符串并返回Car。 To create a Car variable named honda, you need to call the constructor with actual values:
要创建一个名为honda的Car变量,您需要使用实际值调用构造函数:
Car honda(2005, "Accord");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.