[英]C++ error C2228
我承認我不確定自己在這里做什么,所以我做了很多工作,從教科書中復制示例代碼,並用自己程序的信息代替...但是可以告訴我是什么導致了此錯誤?
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;
}
汽車
// 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”的左側必須具有class / struct / union
錯誤C2228:“。setMake”的左側必須具有class / struct / union
當你說Car honda(int yearModel, string make);
,您要聲明一個名為honda的函數,該函數需要一個int和一個字符串並返回Car。 要創建一個名為honda的Car變量,您需要使用實際值調用構造函數:
Car honda(2005, "Accord");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.