-1

我有一个类构造函数,它接受字符串、结构、枚举和 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++;
}

我不明白我在这里做的错误。帮助将不胜感激。谢谢

4

2 回答 2

2
Car obj(string bini, struct Date dini, enum Status state, int id);

那是一个函数声明。省略您传递的变量的类型。

Car obj(bini, dini, state, id);
于 2013-06-18T10:35:19.543 回答
0

最后我发现了错误是什么。Car.cpp 的结构和枚举应该在全局范围内定义。然后它的变量声明应该在 main.cpp 中完成,如下所示。

//main.cpp
int main()
{
string bini = "";
int id = 0;
TDate dini;    //previously i was using separate temporary struct and enum 
dini.day = 0;  //inside main, that was not necessary. Now since the class Car's
dini.month = 0;  //struct and enum are global i can use them here to declare variables
dini.year = 0;   //for enum, i directly passed the value OK thru constructor
int id = 0;
Car obj1(bini, dini, OK, id);
obj1.getBrand();  //no error now
}
//Car.h

struct TDate    //**global definition**
{
int year;
int month;
int day;
};

enum TStatus{ OK, SOLD, DEFECT };
class Car
{
public:
void getBrand();
Car();
Car(string bini, TDate dini,TStatus sini, int id);
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, TDate dini, 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++;
}

谢谢。

于 2013-06-18T23:24:57.183 回答