我有一个类构造函数,它接受字符串、结构、枚举和 int 作为参数。除此之外,我还有一个默认构造函数。每当我使用使用参数构造函数创建的对象名称调用我的类函数时,我都会收到一些错误消息,指出成员是非类类型。但是,如果我使用默认构造函数对象名称,我不会收到任何错误。
Car obj(string bini, struct Date dini, enum Status state, int id);
obj.getBrand(); //doesn't work. getting non-class type error
Car obj1; //constructor without parameters
obj1.getBrand(); //works fine
这是更详细的代码。
//main.cpp
struct Date
{
int year = 0;
int month = 0;
int day = 0;
};
enum Status{ OK, SOLD, DEFECT };
int main()
{
string bini = "";
int id = 0;
Car obj(string bini, struct Date dini, enum Status state, int id);
obj.getBrand(); //doesn't work. getting non-class type error
// Car obj1; //constructor without parameters
//obj1.getBrand(); //works fine
}
//Car.h
class Car
{
public:
struct TDate
{
int year;
int month;
int day;
};
enum TStatus{ OK, SOLD, DEFECT };
void getID();
void getPrice();
void getBrand();
TStatus getStatus();
TDate getManufactureDate();
void setPrice(float price);
void setStatus(TStatus state);
void sellCar();
Car();
Car(string bini, struct TDate dini, enum TStatus sini, int id);
protected:
private:
TStatus state[100];
string brand[100];
float priceEuro[100];
int carId[100], bc = 0, pc = 0, dc = 0, sc = 0;
TDate manufatureDate[100];
};
//Car.cpp
Car::Car(string bini, struct TDate dini, enum TStatus sini, int id)
{
for(int i = 0; i < 100; i++)
{
brand[i] = {bini};
manufatureDate[i].day = dini.day;
manufatureDate[i].month = dini.month;
manufatureDate[i].year = dini.year;
state[i] = sini;
carId[i] = id;
}
}
void Car::getBrand()
{
cout << "Enter the Brand of the Car : " << endl;
cin >> brand[bc];
carId[bc] = bc;
bc++;
}
我不明白我在这里做的错误。帮助将不胜感激。谢谢